<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FSecurity%2FDES_Data_Encryption_Standard</id>
		<title>Java Tutorial/Security/DES Data Encryption Standard - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FSecurity%2FDES_Data_Encryption_Standard"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Security/DES_Data_Encryption_Standard&amp;action=history"/>
		<updated>2026-04-21T08:59:50Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Security/DES_Data_Encryption_Standard&amp;diff=4374&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Security/DES_Data_Encryption_Standard&amp;diff=4374&amp;oldid=prev"/>
				<updated>2010-06-01T05:02:03Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:02, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Security/DES_Data_Encryption_Standard&amp;diff=4373&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Security/DES_Data_Encryption_Standard&amp;diff=4373&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Basic symmetric encryption example with CTR using DES ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.security.Security;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.spec.IvParameterSpec;&lt;br /&gt;
import javax.crypto.spec.SecretKeySpec;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());&lt;br /&gt;
    byte[] input = &amp;quot;input&amp;quot;.getBytes();&lt;br /&gt;
    byte[] keyBytes = &amp;quot;12345678&amp;quot;.getBytes();&lt;br /&gt;
    byte[] ivBytes = &amp;quot;input123&amp;quot;.getBytes();&lt;br /&gt;
    SecretKeySpec key = new SecretKeySpec(keyBytes, &amp;quot;DES&amp;quot;);&lt;br /&gt;
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(&amp;quot;DES/CTR/NoPadding&amp;quot;, &amp;quot;BC&amp;quot;);&lt;br /&gt;
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);&lt;br /&gt;
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];&lt;br /&gt;
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);&lt;br /&gt;
    ctLength += cipher.doFinal(cipherText, ctLength);&lt;br /&gt;
    System.out.println(&amp;quot;cipher: &amp;quot; + new String(cipherText));&lt;br /&gt;
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);&lt;br /&gt;
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];&lt;br /&gt;
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);&lt;br /&gt;
    ptLength += cipher.doFinal(plainText, ptLength);&lt;br /&gt;
    System.out.println(&amp;quot;plain : &amp;quot; + new String(plainText));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  CBC using DES with an IV based on a nonce. In this case a hypothetical message number. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.security.Security;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.spec.IvParameterSpec;&lt;br /&gt;
import javax.crypto.spec.SecretKeySpec;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());&lt;br /&gt;
    byte[] input = &amp;quot;input&amp;quot;.getBytes();&lt;br /&gt;
    byte[] keyBytes = &amp;quot;input123&amp;quot;.getBytes();&lt;br /&gt;
    byte[] msgNumber = &amp;quot;input&amp;quot;.getBytes();&lt;br /&gt;
    IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);&lt;br /&gt;
    SecretKeySpec key = new SecretKeySpec(keyBytes, &amp;quot;DES&amp;quot;);&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(&amp;quot;DES/CBC/PKCS7Padding&amp;quot;, &amp;quot;BC&amp;quot;);&lt;br /&gt;
    cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);&lt;br /&gt;
    IvParameterSpec encryptionIv = new IvParameterSpec(cipher.doFinal(msgNumber), 0, 8);&lt;br /&gt;
    cipher.init(Cipher.ENCRYPT_MODE, key, encryptionIv);&lt;br /&gt;
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];&lt;br /&gt;
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);&lt;br /&gt;
    ctLength += cipher.doFinal(cipherText, ctLength);&lt;br /&gt;
    System.out.println(&amp;quot;cipher: &amp;quot; + new String(cipherText) + &amp;quot; bytes: &amp;quot; + ctLength);&lt;br /&gt;
    cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);&lt;br /&gt;
    IvParameterSpec decryptionIv = new IvParameterSpec(cipher.doFinal(msgNumber), 0, 8);&lt;br /&gt;
    cipher.init(Cipher.DECRYPT_MODE, key, decryptionIv);&lt;br /&gt;
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];&lt;br /&gt;
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);&lt;br /&gt;
    ptLength += cipher.doFinal(plainText, ptLength);&lt;br /&gt;
    System.out.println(&amp;quot;plain : &amp;quot; + new String(plainText, ptLength));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Decrypt an object with DES ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.ObjectInputStream;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.SealedObject;&lt;br /&gt;
import javax.crypto.SecretKey;&lt;br /&gt;
public class Main {&lt;br /&gt;
  private static Object readFromFile(String filename) throws Exception {&lt;br /&gt;
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(filename)));&lt;br /&gt;
    Object object = ois.readObject();&lt;br /&gt;
    ois.close();&lt;br /&gt;
    return object;&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    SecretKey key = (SecretKey) readFromFile(&amp;quot;secretkey.dat&amp;quot;);&lt;br /&gt;
    SealedObject sealedObject = (SealedObject) readFromFile(&amp;quot;sealed.dat&amp;quot;);&lt;br /&gt;
    String algorithmName = sealedObject.getAlgorithm();&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(algorithmName);&lt;br /&gt;
    cipher.init(Cipher.DECRYPT_MODE, key);&lt;br /&gt;
    String text = (String) sealedObject.getObject(cipher);&lt;br /&gt;
    System.out.println(&amp;quot;Text = &amp;quot; + text);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Encrypt an object with DES ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.ObjectOutputStream;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.KeyGenerator;&lt;br /&gt;
import javax.crypto.SealedObject;&lt;br /&gt;
import javax.crypto.SecretKey;&lt;br /&gt;
public class Main {&lt;br /&gt;
  private static void writeToFile(String filename, Object object) throws Exception {&lt;br /&gt;
   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(filename)));&lt;br /&gt;
    oos.writeObject(object);&lt;br /&gt;
    oos.flush();&lt;br /&gt;
    oos.close();&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    SecretKey key = KeyGenerator.getInstance(&amp;quot;DES&amp;quot;).generateKey();&lt;br /&gt;
    writeToFile(&amp;quot;secretkey.dat&amp;quot;, key);&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(&amp;quot;DES&amp;quot;);&lt;br /&gt;
    cipher.init(Cipher.ENCRYPT_MODE, key);&lt;br /&gt;
    SealedObject sealedObject = new SealedObject(&amp;quot;THIS IS A SECRET MESSAGE!&amp;quot;, cipher);&lt;br /&gt;
    writeToFile(&amp;quot;sealed.dat&amp;quot;, sealedObject);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Encrypting a File or Stream with DES ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
import java.security.spec.AlgorithmParameterSpec;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.CipherInputStream;&lt;br /&gt;
import javax.crypto.CipherOutputStream;&lt;br /&gt;
import javax.crypto.KeyGenerator;&lt;br /&gt;
import javax.crypto.SecretKey;&lt;br /&gt;
import javax.crypto.spec.IvParameterSpec;&lt;br /&gt;
class DesEncrypter {&lt;br /&gt;
  byte[] buf = new byte[1024];&lt;br /&gt;
  Cipher ecipher;&lt;br /&gt;
  Cipher dcipher;&lt;br /&gt;
  DesEncrypter(SecretKey key) throws Exception{&lt;br /&gt;
    byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };&lt;br /&gt;
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);&lt;br /&gt;
    ecipher = Cipher.getInstance(&amp;quot;DES/CBC/PKCS5Padding&amp;quot;);&lt;br /&gt;
    dcipher = Cipher.getInstance(&amp;quot;DES/CBC/PKCS5Padding&amp;quot;);&lt;br /&gt;
    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);&lt;br /&gt;
    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void encrypt(InputStream in, OutputStream out)  throws Exception{&lt;br /&gt;
    out = new CipherOutputStream(out, ecipher);&lt;br /&gt;
    int numRead = 0;&lt;br /&gt;
    while ((numRead = in.read(buf)) &amp;gt;= 0) {&lt;br /&gt;
      out.write(buf, 0, numRead);&lt;br /&gt;
    }&lt;br /&gt;
    out.close();&lt;br /&gt;
  }&lt;br /&gt;
  public void decrypt(InputStream in, OutputStream out)  throws Exception{&lt;br /&gt;
    in = new CipherInputStream(in, dcipher);&lt;br /&gt;
    int numRead = 0;&lt;br /&gt;
    while ((numRead = in.read(buf)) &amp;gt;= 0) {&lt;br /&gt;
      out.write(buf, 0, numRead);&lt;br /&gt;
    }&lt;br /&gt;
    out.close();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    SecretKey key = KeyGenerator.getInstance(&amp;quot;DES&amp;quot;).generateKey();&lt;br /&gt;
    DesEncrypter encrypter = new DesEncrypter(key);&lt;br /&gt;
    encrypter.encrypt(new FileInputStream(&amp;quot;cleartext1&amp;quot;), new FileOutputStream(&amp;quot;ciphertext&amp;quot;));&lt;br /&gt;
    encrypter.decrypt(new FileInputStream(&amp;quot;ciphertext&amp;quot;), new FileOutputStream(&amp;quot;cleartext2&amp;quot;));&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Encrypting a String with DES ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.KeyGenerator;&lt;br /&gt;
import javax.crypto.SecretKey;&lt;br /&gt;
class DesEncrypter {&lt;br /&gt;
  Cipher ecipher;&lt;br /&gt;
  Cipher dcipher;&lt;br /&gt;
  DesEncrypter(SecretKey key) throws Exception {&lt;br /&gt;
    ecipher = Cipher.getInstance(&amp;quot;DES&amp;quot;);&lt;br /&gt;
    dcipher = Cipher.getInstance(&amp;quot;DES&amp;quot;);&lt;br /&gt;
    ecipher.init(Cipher.ENCRYPT_MODE, key);&lt;br /&gt;
    dcipher.init(Cipher.DECRYPT_MODE, key);&lt;br /&gt;
  }&lt;br /&gt;
  public String encrypt(String str) throws Exception {&lt;br /&gt;
    // Encode the string into bytes using utf-8&lt;br /&gt;
    byte[] utf8 = str.getBytes(&amp;quot;UTF8&amp;quot;);&lt;br /&gt;
    // Encrypt&lt;br /&gt;
    byte[] enc = ecipher.doFinal(utf8);&lt;br /&gt;
    // Encode bytes to base64 to get a string&lt;br /&gt;
    return new sun.misc.BASE64Encoder().encode(enc);&lt;br /&gt;
  }&lt;br /&gt;
  public String decrypt(String str) throws Exception {&lt;br /&gt;
    // Decode base64 to get bytes&lt;br /&gt;
    byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);&lt;br /&gt;
    byte[] utf8 = dcipher.doFinal(dec);&lt;br /&gt;
    // Decode using utf-8&lt;br /&gt;
    return new String(utf8, &amp;quot;UTF8&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    SecretKey key = KeyGenerator.getInstance(&amp;quot;DES&amp;quot;).generateKey();&lt;br /&gt;
    DesEncrypter encrypter = new DesEncrypter(key);&lt;br /&gt;
    String encrypted = encrypter.encrypt(&amp;quot;Don&amp;quot;t tell anybody!&amp;quot;);&lt;br /&gt;
    String decrypted = encrypter.decrypt(encrypted);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Encrypting with DES Using a Pass Phrase ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.security.spec.AlgorithmParameterSpec;&lt;br /&gt;
import java.security.spec.KeySpec;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.SecretKey;&lt;br /&gt;
import javax.crypto.SecretKeyFactory;&lt;br /&gt;
import javax.crypto.spec.PBEKeySpec;&lt;br /&gt;
import javax.crypto.spec.PBEParameterSpec;&lt;br /&gt;
import sun.misc.BASE64Decoder;&lt;br /&gt;
import sun.misc.BASE64Encoder;&lt;br /&gt;
class DesEncrypter {&lt;br /&gt;
  Cipher ecipher;&lt;br /&gt;
  Cipher dcipher;&lt;br /&gt;
  byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35,&lt;br /&gt;
      (byte) 0xE3, (byte) 0x03 };&lt;br /&gt;
  DesEncrypter(String passPhrase) throws Exception {&lt;br /&gt;
    int iterationCount = 2;&lt;br /&gt;
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);&lt;br /&gt;
    SecretKey key = SecretKeyFactory.getInstance(&amp;quot;PBEWithMD5AndDES&amp;quot;).generateSecret(keySpec);&lt;br /&gt;
    ecipher = Cipher.getInstance(key.getAlgorithm());&lt;br /&gt;
    dcipher = Cipher.getInstance(key.getAlgorithm());&lt;br /&gt;
    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);&lt;br /&gt;
    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);&lt;br /&gt;
    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);&lt;br /&gt;
  }&lt;br /&gt;
  public String encrypt(String str) throws Exception {&lt;br /&gt;
    return new BASE64Encoder().encode(ecipher.doFinal(str.getBytes()));&lt;br /&gt;
  }&lt;br /&gt;
  public String decrypt(String str) throws Exception {&lt;br /&gt;
    return new String(dcipher.doFinal(new BASE64Decoder().decodeBuffer(str)));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    DesEncrypter encrypter = new DesEncrypter(&amp;quot;My Pass Phrase!&amp;quot;);&lt;br /&gt;
    String encrypted = encrypter.encrypt(&amp;quot;Don&amp;quot;t tell anybody!&amp;quot;);&lt;br /&gt;
    String decrypted = encrypter.decrypt(encrypted);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Message without tampering with MAC (DES), encryption AES in CTR mode ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.security.Key;&lt;br /&gt;
import java.security.MessageDigest;&lt;br /&gt;
import java.security.NoSuchAlgorithmException;&lt;br /&gt;
import java.security.NoSuchProviderException;&lt;br /&gt;
import java.security.SecureRandom;&lt;br /&gt;
import java.security.Security;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.KeyGenerator;&lt;br /&gt;
import javax.crypto.Mac;&lt;br /&gt;
import javax.crypto.SecretKey;&lt;br /&gt;
import javax.crypto.spec.IvParameterSpec;&lt;br /&gt;
import javax.crypto.spec.SecretKeySpec;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());&lt;br /&gt;
    SecureRandom random = new SecureRandom();&lt;br /&gt;
    IvParameterSpec ivSpec = createCtrIvForAES();&lt;br /&gt;
    Key key = createKeyForAES(256, random);&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(&amp;quot;AES/CTR/NoPadding&amp;quot;, &amp;quot;BC&amp;quot;);&lt;br /&gt;
    String input = &amp;quot;12345678&amp;quot;;&lt;br /&gt;
    Mac mac = Mac.getInstance(&amp;quot;DES&amp;quot;, &amp;quot;BC&amp;quot;);&lt;br /&gt;
    byte[] macKeyBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };&lt;br /&gt;
    Key macKey = new SecretKeySpec(macKeyBytes, &amp;quot;DES&amp;quot;);&lt;br /&gt;
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);&lt;br /&gt;
    byte[] cipherText = new byte[cipher.getOutputSize(input.length() + mac.getMacLength())];&lt;br /&gt;
    int ctLength = cipher.update(input.getBytes(), 0, input.length(), cipherText, 0);&lt;br /&gt;
    mac.init(macKey);&lt;br /&gt;
    mac.update(input.getBytes());&lt;br /&gt;
    ctLength += cipher.doFinal(mac.doFinal(), 0, mac.getMacLength(), cipherText, ctLength);&lt;br /&gt;
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);&lt;br /&gt;
    byte[] plainText = cipher.doFinal(cipherText, 0, ctLength);&lt;br /&gt;
    int messageLength = plainText.length - mac.getMacLength();&lt;br /&gt;
    mac.init(macKey);&lt;br /&gt;
    mac.update(plainText, 0, messageLength);&lt;br /&gt;
    byte[] messageHash = new byte[mac.getMacLength()];&lt;br /&gt;
    System.arraycopy(plainText, messageLength, messageHash, 0, messageHash.length);&lt;br /&gt;
    System.out.println(&amp;quot;plain : &amp;quot; + new String(plainText) + &amp;quot; verified: &amp;quot;&lt;br /&gt;
        + MessageDigest.isEqual(mac.doFinal(), messageHash));&lt;br /&gt;
  }&lt;br /&gt;
  public static SecretKey createKeyForAES(int bitLength, SecureRandom random)&lt;br /&gt;
      throws NoSuchAlgorithmException, NoSuchProviderException {&lt;br /&gt;
    KeyGenerator generator = KeyGenerator.getInstance(&amp;quot;AES&amp;quot;, &amp;quot;BC&amp;quot;);&lt;br /&gt;
    generator.init(128, random);&lt;br /&gt;
    return generator.generateKey();&lt;br /&gt;
  }&lt;br /&gt;
  public static IvParameterSpec createCtrIvForAES() {&lt;br /&gt;
    return new IvParameterSpec(&amp;quot;1234567812345678&amp;quot;.getBytes());&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>