主题: Java加密技术(三)
精华帖 (0)   良好帖 (0)   新手帖(0)   垃圾帖 (0)      收藏
  • ppto 我现在不在线,你找我吗?
  • 显示默认头像
  • 昵称:ppto
  • 专家等级:新手上路
  • 专家分:0
  • 可用分等级:渔夫
  • 精华:0
  • 帖子数:137
  • 结帖率: 100%
  • 注册时间:2009-04-29 09:35:51
发表于 2009-05-14 10:08:04
楼主

 Java加密技术(三)

      [关键字: Java]

除了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.     public static final String ALGORITHM = "PBEWITHMD5andDES";   
  19.   
  20.     /**  
  21.      * 盐初始化  
  22.      *   
  23.      * @return  
  24.      * @throws Exception  
  25.      */  
  26.     public static byte[] initSalt() throws Exception {   
  27.         byte[] salt = new byte[8];   
  28.         Random random = new Random();   
  29.         random.nextBytes(salt);   
  30.         return salt;   
  31.     }   
  32.   
  33.     /**  
  34.      * 转换密钥<br>  
  35.      *   
  36.      * @param key  
  37.      * @return  
  38.      * @throws Exception  
  39.      */  
  40.     private static Key toKey(char[] key) throws Exception {   
  41.         PBEKeySpec keySpec = new PBEKeySpec(key);   
  42.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);   
  43.         SecretKey secretKey = keyFactory.generateSecret(keySpec);   
  44.   
  45.         return secretKey;   
  46.     }   
  47.   
  48.     /**  
  49.      * 加密  
  50.      *   
  51.      * @param data  
  52.      *            数据  
  53.      * @param pwd  
  54.      *            密码  
  55.      * @param salt  
  56.      *            盐  
  57.      * @return  
  58.      * @throws Exception  
  59.      */  
  60.     public static byte[] encrypt(byte[] data, String pwd, byte[] salt)   
  61.             throws Exception {   
  62.         char[] password = pwd.toCharArray();   
  63.   
  64.         Key key = toKey(password);   
  65.   
  66.         PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);   
  67.         Cipher cipher = Cipher.getInstance(ALGORITHM);   
  68.         cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);   
  69.   
  70.         return cipher.doFinal(data);   
  71.   
  72.     }   
  73.   
  74.     /**  
  75.      * 解密  
  76.      *   
  77.      * @param data  
  78.      *            数据  
  79.      * @param pwd  
  80.      *            密码  
  81.      * @param salt  
  82.      *            盐  
  83.      * @return  
  84.      * @throws Exception  
  85.      */  
  86.     public static byte[] decrypt(byte[] data, String pwd, byte[] salt)   
  87.             throws Exception {   
  88.         char[] password = pwd.toCharArray();   
  89.   
  90.         Key key = toKey(password);   
  91.   
  92.         PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);   
  93.         Cipher cipher = Cipher.getInstance(ALGORITHM);   
  94.         cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);   
  95.   
  96.         return cipher.doFinal(data);   
  97.   
  98.     }   
  99. }  



再给出一个测试类:
 

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等。


快速回复主题
您还未登录,不能回复帖子
phome.asia   程序员之家论坛
程序员之家 版权所有 Copyright 2004-2009 All Rights Reserved©2009 京 ICP 备 05027197 号 网站地图 关于我们 联系我们