转载:Java加密技术(三)--- PBE

http://blog.163.com/niwei_258/blog/static/106284882010111022226119/
除了DES,我们还知道有DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)等多种对称加密方式,其实现方式大同小异,这里介绍对称加密的另一个算法——PBE 

PBE 
   PBE——Password-basedencryption(基于密码加密)。其特点在于口令由用户自己掌管,不借助任何物理媒体;采用随机数(这里我们叫做盐)杂凑多重加密等方法保证数据的安全性。是一种简便的加密方式。 

【密钥算法】Java加密技术(三)---PBE 数据加密算法 - 八月照相馆 - 八月照相馆
 

通过java代码实现如下:Coder类见 Java加密技术(一) 

Java代码 
  1. import java.security.Key;  
  2. import java.util.Random;  
  3.   
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.SecretKey;  
  6. import javax.crypto.SecretKeyFactory;  
  7. import javax.crypto.spec.PBEKeySpec;  
  8. import javax.crypto.spec.PBEParameterSpec;  
  9.   
  10.   
  11. public abstract class PBECoder extends Coder {  
  12.       
  13.     public static final String ALGORITHM = "PBEWITHMD5andDES";  
  14.   
  15.       
  16.     public static byte[] initSalt() throws Exception {  
  17.         byte[] salt = new byte[8];  
  18.         Random random = new Random();  
  19.         random.nextBytes(salt);  
  20.         return salt;  
  21.     }  
  22.   
  23.       
  24.     private static Key toKey(String password) throws Exception {  
  25.         PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());  
  26.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);  
  27.         SecretKey secretKey = keyFactory.generateSecret(keySpec);  
  28.   
  29.         return secretKey;  
  30.     }  
  31.   
  32.       
  33.     public static byte[] encrypt(byte[] data, String password, byte[] salt)  
  34.             throws Exception {  
  35.   
  36.         Key key = toKey(password);  
  37.   
  38.         PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);  
  39.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  40.         cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);  
  41.   
  42.         return cipher.doFinal(data);  
  43.   
  44.     }  
  45.   
  46.       
  47.     public static byte[] decrypt(byte[] data, String password, byte[] salt)  
  48.             throws Exception {  
  49.   
  50.         Key key = toKey(password);  
  51.   
  52.         PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);  
  53.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  54.         cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);  
  55.   
  56.         return cipher.doFinal(data);  
  57.   
  58.     }  
  59. }  


再给出一个测试类: 
Java代码 
  1. import static org.junit.Assert.*;  
  2.   
  3. import org.junit.Test;  
  4.   
  5.   
  6. public class PBECoderTest {  
  7.   
  8.     @Test  
  9.     public void test() throws Exception {  
  10.         String inputStr = "abc";  
  11.         System.err.println("原文: " + inputStr);  
  12.         byte[] input = inputStr.getBytes();  
  13.   
  14.         String pwd = "efg";  
  15.         System.err.println("密码: " + pwd);  
  16.   
  17.         byte[] salt = PBECoder.initSalt();  
  18.   
  19.         byte[] data = PBECoder.encrypt(input, pwd, salt);  
  20.   
  21.         System.err.println("加密后: " + PBECoder.encryptBASE64(data));  
  22.   
  23.         byte[] output = PBECoder.decrypt(data, pwd, salt);  
  24.         String outputStr = new String(output);  
  25.   
  26.         System.err.println("解密后: " + outputStr);  
  27.         assertEquals(inputStr, outputStr);  
  28.     }  
  29.   
  30. }  


控制台输出: 
Console代码 
  1. 原文: abc  
  2. 密码: efg  
  3. 加密后: iCZ0uRtaAhE=  
  4.   
  5. 解密后: abc  

   后续我们会介绍非对称加密算法,如RSA、DSA、DH、ECC等。 

 

posted @ 2014-05-28 08:45  lihui1625  阅读(125)  评论(0编辑  收藏  举报