Java加密技術(三)——PBE算法

轉自:http://snowolf.iteye.com/blog/380761

除了DES,我們還知道有DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)等多種對稱加密方式,其實現方式大同小異,這裏介紹對稱加密的另一個算法——PBE


PBE
    PBE——Password-based encryption(基於密碼加密)。其特點在於口令由用戶自己掌管,不借助任何物理媒體;採用隨機數(這裏我們叫做鹽)雜湊多重加密等方法保證數據的安全性。是一種簡便的加密方式。



通過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.  * PBE安全編碼組件 
  12.  *  
  13.  * @author 樑棟 
  14.  * @version 1.0 
  15.  * @since 1.0 
  16.  */  
  17. public abstract class PBECoder extends Coder {  
  18.     /** 
  19.      * 支持以下任意一種算法 
  20.      *  
  21.      * <pre> 
  22.      * PBEWithMD5AndDES  
  23.      * PBEWithMD5AndTripleDES  
  24.      * PBEWithSHA1AndDESede 
  25.      * PBEWithSHA1AndRC2_40 
  26.      * </pre> 
  27.      */  
  28.     public static final String ALGORITHM = "PBEWITHMD5andDES";  
  29.   
  30.     /** 
  31.      * 鹽初始化 
  32.      *  
  33.      * @return 
  34.      * @throws Exception 
  35.      */  
  36.     public static byte[] initSalt() throws Exception {  
  37.         byte[] salt = new byte[8];  
  38.         Random random = new Random();  
  39.         random.nextBytes(salt);  
  40.         return salt;  
  41.     }  
  42.   
  43.     /** 
  44.      * 轉換密鑰<br> 
  45.      *  
  46.      * @param password 
  47.      * @return 
  48.      * @throws Exception 
  49.      */  
  50.     private static Key toKey(String password) throws Exception {  
  51.         PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());  
  52.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);  
  53.         SecretKey secretKey = keyFactory.generateSecret(keySpec);  
  54.   
  55.         return secretKey;  
  56.     }  
  57.   
  58.     /** 
  59.      * 加密 
  60.      *  
  61.      * @param data 
  62.      *            數據 
  63.      * @param password 
  64.      *            密碼 
  65.      * @param salt 
  66.      *            鹽 
  67.      * @return 
  68.      * @throws Exception 
  69.      */  
  70.     public static byte[] encrypt(byte[] data, String password, byte[] salt)  
  71.             throws Exception {  
  72.   
  73.         Key key = toKey(password);  
  74.   
  75.         PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);  
  76.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  77.         cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);  
  78.   
  79.         return cipher.doFinal(data);  
  80.   
  81.     }  
  82.   
  83.     /** 
  84.      * 解密 
  85.      *  
  86.      * @param data 
  87.      *            數據 
  88.      * @param password 
  89.      *            密碼 
  90.      * @param salt 
  91.      *            鹽 
  92.      * @return 
  93.      * @throws Exception 
  94.      */  
  95.     public static byte[] decrypt(byte[] data, String password, byte[] salt)  
  96.             throws Exception {  
  97.   
  98.         Key key = toKey(password);  
  99.   
  100.         PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);  
  101.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  102.         cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);  
  103.   
  104.         return cipher.doFinal(data);  
  105.   
  106.     }  
  107. }  


再給出一個測試類:
Java代碼  收藏代碼
  1. import static org.junit.Assert.*;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. /** 
  6.  *  
  7.  * @author 樑棟 
  8.  * @version 1.0 
  9.  * @since 1.0 
  10.  */  
  11. public class PBECoderTest {  
  12.   
  13.     @Test  
  14.     public void test() throws Exception {  
  15.         String inputStr = "abc";  
  16.         System.err.println("原文: " + inputStr);  
  17.         byte[] input = inputStr.getBytes();  
  18.   
  19.         String pwd = "efg";  
  20.         System.err.println("密碼: " + pwd);  
  21.   
  22.         byte[] salt = PBECoder.initSalt();  
  23.   
  24.         byte[] data = PBECoder.encrypt(input, pwd, salt);  
  25.   
  26.         System.err.println("加密後: " + PBECoder.encryptBASE64(data));  
  27.   
  28.         byte[] output = PBECoder.decrypt(data, pwd, salt);  
  29.         String outputStr = new String(output);  
  30.   
  31.         System.err.println("解密後: " + outputStr);  
  32.         assertEquals(inputStr, outputStr);  
  33.     }  
  34.   
  35. }  


控制檯輸出:
Console代碼  收藏代碼
  1. 原文: abc  
  2. 密碼: efg  
  3. 加密後: iCZ0uRtaAhE=  
  4.   
  5. 解密後: abc  

    後續我們會介紹非對稱加密算法,如RSA、DSA、DH、ECC等。
發佈了13 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章