Java加密技术(二)——对称加密算法DES&AES

  接下来我们介绍对称加密算法,最常用的莫过于DES数据加密算法。
DES
DES-Data Encryption Standard,即数据加密算法。是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
  DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位。



通过java代码实现如下: Coder类见 Java加密技术(一)
Java代码   收藏代码
  1. import java.security.Key;  
  2. import java.security.SecureRandom;  
  3.   
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.KeyGenerator;  
  6. import javax.crypto.SecretKey;  
  7. import javax.crypto.SecretKeyFactory;  
  8. import javax.crypto.spec.DESKeySpec;  
  9.   
  10.   
  11. /** 
  12.  * DES安全编码组件 
  13.  *  
  14.  * <pre> 
  15.  * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR) 
  16.  * DES                  key size must be equal to 56 
  17.  * DESede(TripleDES)    key size must be equal to 112 or 168 
  18.  * AES                  key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available 
  19.  * Blowfish             key size must be multiple of 8, and can only range from 32 to 448 (inclusive) 
  20.  * RC2                  key size must be between 40 and 1024 bits 
  21.  * RC4(ARCFOUR)         key size must be between 40 and 1024 bits 
  22.  * 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html 
  23.  * </pre> 
  24.  *  
  25.  * @author 梁栋 
  26.  * @version 1.0 
  27.  * @since 1.0 
  28.  */  
  29. public abstract class DESCoder extends Coder {  
  30.     /** 
  31.      * ALGORITHM 算法 <br> 
  32.      * 可替换为以下任意一种算法,同时key值的size相应改变。 
  33.      *  
  34.      * <pre> 
  35.      * DES                  key size must be equal to 56 
  36.      * DESede(TripleDES)    key size must be equal to 112 or 168 
  37.      * AES                  key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available 
  38.      * Blowfish             key size must be multiple of 8, and can only range from 32 to 448 (inclusive) 
  39.      * RC2                  key size must be between 40 and 1024 bits 
  40.      * RC4(ARCFOUR)         key size must be between 40 and 1024 bits 
  41.      * </pre> 
  42.      *  
  43.      * 在Key toKey(byte[] key)方法中使用下述代码 
  44.      * <code>SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);</code> 替换 
  45.      * <code> 
  46.      * DESKeySpec dks = new DESKeySpec(key); 
  47.      * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); 
  48.      * SecretKey secretKey = keyFactory.generateSecret(dks); 
  49.      * </code> 
  50.      */  
  51.     public static final String ALGORITHM = "DES";  
  52.   
  53.     /** 
  54.      * 转换密钥<br> 
  55.      *  
  56.      * @param key 
  57.      * @return 
  58.      * @throws Exception 
  59.      */  
  60.     private static Key toKey(byte[] key) throws Exception {  
  61.         DESKeySpec dks = new DESKeySpec(key);  
  62.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);  
  63.         SecretKey secretKey = keyFactory.generateSecret(dks);  
  64.   
  65.         // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码  
  66.         // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);  
  67.   
  68.         return secretKey;  
  69.     }  
  70.   
  71.     /** 
  72.      * 解密 
  73.      *  
  74.      * @param data 
  75.      * @param key 
  76.      * @return 
  77.      * @throws Exception 
  78.      */  
  79.     public static byte[] decrypt(byte[] data, String key) throws Exception {  
  80.         Key k = toKey(decryptBASE64(key));  
  81.   
  82.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  83.         cipher.init(Cipher.DECRYPT_MODE, k);  
  84.   
  85.         return cipher.doFinal(data);  
  86.     }  
  87.   
  88.     /** 
  89.      * 加密 
  90.      *  
  91.      * @param data 
  92.      * @param key 
  93.      * @return 
  94.      * @throws Exception 
  95.      */  
  96.     public static byte[] encrypt(byte[] data, String key) throws Exception {  
  97.         Key k = toKey(decryptBASE64(key));  
  98.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  99.         cipher.init(Cipher.ENCRYPT_MODE, k);  
  100.   
  101.         return cipher.doFinal(data);  
  102.     }  
  103.   
  104.     /** 
  105.      * 生成密钥 
  106.      *  
  107.      * @return 
  108.      * @throws Exception 
  109.      */  
  110.     public static String initKey() throws Exception {  
  111.         return initKey(null);  
  112.     }  
  113.   
  114.     /** 
  115.      * 生成密钥 
  116.      *  
  117.      * @param seed 
  118.      * @return 
  119.      * @throws Exception 
  120.      */  
  121.     public static String initKey(String seed) throws Exception {  
  122.         SecureRandom secureRandom = null;  
  123.   
  124.         if (seed != null) {  
  125.             secureRandom = new SecureRandom(decryptBASE64(seed));  
  126.         } else {  
  127.             secureRandom = new SecureRandom();  
  128.         }  
  129.   
  130.         KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);  
  131.         kg.init(secureRandom);  
  132.   
  133.         SecretKey secretKey = kg.generateKey();  
  134.   
  135.         return encryptBASE64(secretKey.getEncoded());  
  136.     }  
  137. }  

延续上一个类的实现,我们通过MD5以及SHA对字符串加密生成密钥,这是比较常见的密钥生成方式。
再给出一个测试类:
Java代码   收藏代码
  1. import static org.junit.Assert.*;  
  2.   
  3.   
  4. import org.junit.Test;  
  5.   
  6. /** 
  7.  *  
  8.  * @author 梁栋 
  9.  * @version 1.0 
  10.  * @since 1.0 
  11.  */  
  12. public class DESCoderTest {  
  13.   
  14.     @Test  
  15.     public void test() throws Exception {  
  16.         String inputStr = "DES";  
  17.         String key = DESCoder.initKey();  
  18.         System.err.println("原文:\t" + inputStr);  
  19.   
  20.         System.err.println("密钥:\t" + key);  
  21.   
  22.         byte[] inputData = inputStr.getBytes();  
  23.         inputData = DESCoder.encrypt(inputData, key);  
  24.   
  25.         System.err.println("加密后:\t" + DESCoder.encryptBASE64(inputData));  
  26.   
  27.         byte[] outputData = DESCoder.decrypt(inputData, key);  
  28.         String outputStr = new String(outputData);  
  29.   
  30.         System.err.println("解密后:\t" + outputStr);  
  31.   
  32.         assertEquals(inputStr, outputStr);  
  33.     }  
  34. }  

得到的输出内容如下:
Console代码   收藏代码
  1. 原文: DES  
  2. 密钥: f3wEtRrV6q0=  
  3.   
  4. 加密后:    C6qe9oNIzRY=  
  5.   
  6. 解密后:    DES  

    由控制台得到的输出,我们能够比对加密、解密后结果一致。这是一种简单的加密解密方式,只有一个密钥。
    其实DES有很多同胞兄弟,如DESede(TripleDES)、AES、Blowfish、RC2、RC4(ARCFOUR)。这里就不过多阐述了,大同小异,只要换掉ALGORITHM换成对应的值,同时做一个代码替换 SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);就可以了,此外就是密钥长度不同了。

Java代码  
  1. /** 
  2.  * DES          key size must be equal to 56 
  3.  * DESede(TripleDES) key size must be equal to 112 or 168 
  4.  * AES          key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available 
  5.  * Blowfish     key size must be multiple of 8, and can only range from 32 to 448 (inclusive) 
  6.  * RC2          key size must be between 40 and 1024 bits 
  7.  * RC4(ARCFOUR) key size must be between 40 and 1024 bits 
  8.  **/ 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值