现在的位置: 首页 > 综合 > 正文

对称加密算法

2013年11月17日 ⁄ 综合 ⁄ 共 4885字 ⁄ 字号 评论关闭

 

原创作者: snowolf  

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代码

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

延续上一个类的实现,我们通过MD5以及SHA对字符串加密生成密钥,这是比较常见的密钥生成方式。 
再给出一个测试类:

Java代码

  1. import static org.junit.Assert.*; 
  2. import org.junit.Test; 
  3. /**
  4. *
  5. * @author 梁栋
  6. * @version 1.0
  7. * @since 1.0
  8. */
  9. public class DESCoderTest { 
  10. @Test
  11. public void test() throws Exception { 
  12.         String inputStr = "DES"; 
  13.         String key = DESCoder.initKey(); 
  14.         System.err.println("原文:/t" + inputStr); 
  15.         System.err.println("密钥:/t" + key); 
  16. byte[] inputData = inputStr.getBytes(); 
  17.         inputData = DESCoder.encrypt(inputData, key); 
  18.         System.err.println("加密后:/t" + DESCoder.encryptBASE64(inputData)); 
  19. byte[] outputData = DESCoder.decrypt(inputData, key); 
  20.         String outputStr = new String(outputData); 
  21.         System.err.println("解密后:/t" + outputStr); 
  22.         assertEquals(inputStr, outputStr); 
  23.     } 

得到的输出内容如下:

Console代码

  1. 原文: DES 
  2. 密钥: f3wEtRrV6q0= 
  3. 加密后:    C6qe9oNIzRY= 
  4. 解密后:    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. **/

 

抱歉!评论已关闭.