<?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%2FPassword_Based_Encryption</id>
		<title>Java Tutorial/Security/Password Based Encryption - История изменений</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%2FPassword_Based_Encryption"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Security/Password_Based_Encryption&amp;action=history"/>
		<updated>2026-04-21T13:45:23Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Security/Password_Based_Encryption&amp;diff=4336&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/Password_Based_Encryption&amp;diff=4336&amp;oldid=prev"/>
				<updated>2010-06-01T05:01:44Z</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:01, 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/Password_Based_Encryption&amp;diff=4335&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/Password_Based_Encryption&amp;diff=4335&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;==  PBE File Encrypt ==&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.BufferedOutputStream;&lt;br /&gt;
import java.io.FileInputStream;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.security.MessageDigest;&lt;br /&gt;
import javax.crypto.Cipher;&lt;br /&gt;
import javax.crypto.CipherInputStream;&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;
public class MainClass  {&lt;br /&gt;
  public static void main(String[] args) throws Exception{&lt;br /&gt;
    Cipher cipher = createCipher(Cipher.ENCRYPT_MODE);&lt;br /&gt;
    applyCipher(&amp;quot;file_to_encrypt&amp;quot;, &amp;quot;encrypted_file&amp;quot;, cipher);&lt;br /&gt;
    cipher = createCipher(Cipher.DECRYPT_MODE);&lt;br /&gt;
    applyCipher(&amp;quot;file_to_decrypt&amp;quot;, &amp;quot;decrypted_file&amp;quot;, cipher);&lt;br /&gt;
  }&lt;br /&gt;
  static Cipher createCipher(int mode) throws Exception {&lt;br /&gt;
    PBEKeySpec keySpec = new PBEKeySpec(&amp;quot;test&amp;quot;.toCharArray());&lt;br /&gt;
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(&amp;quot;PBEWithMD5AndDES&amp;quot;);&lt;br /&gt;
    SecretKey key = keyFactory.generateSecret(keySpec);&lt;br /&gt;
    MessageDigest md = MessageDigest.getInstance(&amp;quot;MD5&amp;quot;);&lt;br /&gt;
    md.update(&amp;quot;input&amp;quot;.getBytes());&lt;br /&gt;
    byte[] digest = md.digest();&lt;br /&gt;
    byte[] salt = new byte[8];&lt;br /&gt;
    for (int i = 0; i &amp;lt; 8; ++i)&lt;br /&gt;
      salt[i] = digest[i];&lt;br /&gt;
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(&amp;quot;PBEWithMD5AndDES&amp;quot;);&lt;br /&gt;
    cipher.init(mode, key, paramSpec);&lt;br /&gt;
    return cipher;&lt;br /&gt;
  }&lt;br /&gt;
  static void applyCipher(String inFile, String outFile, Cipher cipher) throws Exception {&lt;br /&gt;
    CipherInputStream in = new CipherInputStream(new FileInputStream(inFile), cipher);&lt;br /&gt;
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));&lt;br /&gt;
    int BUFFER_SIZE = 8;&lt;br /&gt;
    byte[] buffer = new byte[BUFFER_SIZE];&lt;br /&gt;
    int numRead = 0;&lt;br /&gt;
    do {&lt;br /&gt;
      numRead = in.read(buffer);&lt;br /&gt;
      if (numRead &amp;gt; 0)&lt;br /&gt;
        out.write(buffer, 0, numRead);&lt;br /&gt;
    } while (numRead == 8);&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;
==  PBE With SHA And Two fish ==&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.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;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
    System.out.println(decrypt(new char[] { &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot; }, &amp;quot;plaintext1234567890&amp;quot;));&lt;br /&gt;
  }&lt;br /&gt;
  private static String decrypt(char[] password, String text) throws Exception {&lt;br /&gt;
    String salt = text.substring(0, 12);&lt;br /&gt;
    String ciphertext = text.substring(12, text.length());&lt;br /&gt;
    BASE64Decoder decoder = new BASE64Decoder();&lt;br /&gt;
    byte[] saltArray = decoder.decodeBuffer(salt);&lt;br /&gt;
    byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);&lt;br /&gt;
    PBEKeySpec keySpec = new PBEKeySpec(password);&lt;br /&gt;
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(&amp;quot;PBEWithSHAAndTwofish-CBC&amp;quot;);&lt;br /&gt;
    SecretKey key = keyFactory.generateSecret(keySpec);&lt;br /&gt;
    PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, 1000);&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(&amp;quot;PBEWithSHAAndTwofish-CBC&amp;quot;);&lt;br /&gt;
    cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);&lt;br /&gt;
    return new String(cipher.doFinal(ciphertextArray));&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;
==  Read the first 8 bytes of the ciphertext and use that as the salt ==&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.util.Random;&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.BASE64Encoder;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) throws Exception {&lt;br /&gt;
   System.out.println(encrypt(new char[] { &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot; }, &amp;quot;plaintext1234567890&amp;quot;));&lt;br /&gt;
  }&lt;br /&gt;
  private static String encrypt(char[] password, String plaintext) throws Exception {&lt;br /&gt;
    byte[] salt = new byte[8];&lt;br /&gt;
    Random random = new Random();&lt;br /&gt;
    random.nextBytes(salt);&lt;br /&gt;
    PBEKeySpec keySpec = new PBEKeySpec(password);&lt;br /&gt;
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(&amp;quot;PBEWithSHAAndTwofish-CBC&amp;quot;);&lt;br /&gt;
    SecretKey key = keyFactory.generateSecret(keySpec);&lt;br /&gt;
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000);&lt;br /&gt;
    Cipher cipher = Cipher.getInstance(&amp;quot;PBEWithSHAAndTwofish-CBC&amp;quot;);&lt;br /&gt;
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);&lt;br /&gt;
    byte[] ciphertext = cipher.doFinal(plaintext.getBytes());&lt;br /&gt;
    BASE64Encoder encoder = new BASE64Encoder();&lt;br /&gt;
    String saltString = encoder.encode(salt);&lt;br /&gt;
    String ciphertextString = encoder.encode(ciphertext);&lt;br /&gt;
    return saltString + ciphertextString;&lt;br /&gt;
  }&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>