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

Posted on

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. 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. }
  120. }

import java.security.Key;

import java.security.SecureRandom;

import javax.crypto.Cipher; import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

///

/ DES安全编码组件 /

/

/ 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)

/ DES key size must be equal to 56 / DESede(TripleDES) key size must be equal to 112 or 168

/ AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available / Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)

/ RC2 key size must be between 40 and 1024 bits / RC4(ARCFOUR) key size must be between 40 and 1024 bits

/ 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html /

/ / @author 梁栋

/ @version 1.0 / @since 1.0

/*/ public abstract class DESCoder extends Coder {

//*/*
 /* ALGORITHM 算法 <br>

 /* 可替换为以下任意一种算法,同时key值的size相应改变。
 /*

 /* <pre>
 /* DES                  key size must be equal to 56

 /* DESede(TripleDES)     key size must be equal to 112 or 168
 /* AES                  key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available

 /* Blowfish             key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
 /* RC2                  key size must be between 40 and 1024 bits

 /* RC4(ARCFOUR)         key size must be between 40 and 1024 bits
 /* </pre>

 /*
 /* 在Key toKey(byte[] key)方法中使用下述代码

 /* <code>SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);</code> 替换


 /* <code>
 /* DESKeySpec dks = new DESKeySpec(key);

 /* SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
 /* SecretKey secretKey = keyFactory.generateSecret(dks);

 /* </code>
 /*/

public static final String ALGORITHM = "DES";


//*/*
 /* 转换密钥<br>

 /*
 /* @param key

 /* @return
 /* @throws Exception

 /*/
private static Key toKey(byte[] key) throws Exception {

    DESKeySpec dks = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);

    SecretKey secretKey = keyFactory.generateSecret(dks);


    // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码
    // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);


    return secretKey;

}


//*/*
 /* 解密

 /*
 /* @param data

 /* @param key
 /* @return

 /* @throws Exception
 /*/

public static byte[] decrypt(byte[] data, String key) throws Exception {
    Key k = toKey(decryptBASE64(key));


    Cipher cipher = Cipher.getInstance(ALGORITHM);

    cipher.init(Cipher.DECRYPT_MODE, k);


    return cipher.doFinal(data);
}


//*/*

 /* 加密
 /*

 /* @param data
 /* @param key

 /* @return
 /* @throws Exception

 /*/
public static byte[] encrypt(byte[] data, String key) throws Exception {

    Key k = toKey(decryptBASE64(key));
    Cipher cipher = Cipher.getInstance(ALGORITHM);

    cipher.init(Cipher.ENCRYPT_MODE, k);


    return cipher.doFinal(data);
}


//*/*

 /* 生成密钥
 /*

 /* @return
 /* @throws Exception

 /*/
public static String initKey() throws Exception {

    return initKey(null);
}


//*/*

 /* 生成密钥
 /*

 /* @param seed
 /* @return

 /* @throws Exception
 /*/

public static String initKey(String seed) throws Exception {
    SecureRandom secureRandom = null;


    if (seed != null) {

        secureRandom = new SecureRandom(decryptBASE64(seed));
    } else {

        secureRandom = new SecureRandom();
    }


    KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);

    kg.init(secureRandom);


    SecretKey secretKey = kg.generateKey();


    return encryptBASE64(secretKey.getEncoded());
}

} 延续上一个类的实现,我们通过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. }
  24. }

import static org.junit.Assert./*;

import org.junit.Test;

/// /*

/ @author 梁栋 / @version 1.0

/ @since 1.0 //

public class DESCoderTest {

@Test
public void test() throws Exception {

    String inputStr = "DES";
    String key = DESCoder.initKey();

    System.err.println("原文:\t" + inputStr);


    System.err.println("密钥:\t" + key);


    byte[] inputData = inputStr.getBytes();
    inputData = DESCoder.encrypt(inputData, key);


    System.err.println("加密后:\t" + DESCoder.encryptBASE64(inputData));


    byte[] outputData = DESCoder.decrypt(inputData, key);

    String outputStr = new String(outputData);


    System.err.println("解密后:\t" + outputStr);


    assertEquals(inputStr, outputStr);
}

} 得到的输出内容如下: Console代码 复制代码 收藏代码

  1. 原文: DES
  2. 密钥: f3wEtRrV6q0=
  3. 加密后: C6qe9oNIzRY=
  4. 解密后: DES

原文: DES

密钥: f3wEtRrV6q0=

加密后: C6qe9oNIzRY=

解密后: 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. ///

///

/ DES key size must be equal to 56 / DESede(TripleDES) key size must be equal to 112 or 168

/ AES key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available / Blowfish key size must be multiple of 8, and can only range from 32 to 448 (inclusive)

/ RC2 key size must be between 40 and 1024 bits / RC4(ARCFOUR) key size must be between 40 and 1024 bits

/// 相关链接: Java加密技术(一)——BASE64与单向加密算法MD5&SHA&MAC Java加密技术(二)——对称加密算法DES&AES Java加密技术(三)——PBE算法 Java加密技术(四)——非对称加密算法RSA Java加密技术(五)——非对称加密算法的由来 Java加密技术(六)——数字签名算法DSA Java加密技术(七)——非对称加密算法最高ECC Java加密技术(八)——数字证书 Java加密技术(九)——初探SSL Java加密技术(十)——单向认证 Java加密技术(十一)——双向认证 Java加密技术(十二)——/.PFX(/.p12)&个人信息交换文件

希望本站内容对您有点用处,有什么疑问或建议请在后面留言评论
转载请注明作者(RobinChia)和出处 It so life ,请勿用于任何商业用途