1、基于密码加密 http:/ snowolf 阅读:689 次 评论:0 条 更新时间:2009-05-27 除了 DES,我们还知道有 DESede(TripleDES,就是 3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)等多种对称加密方式,其实现方式大同小异,这里介绍对称加密的另一个算法PBE PBE PBEPassword-based encryption(基于密码加密)。其特点在于口令由用户自己掌管,不借助任何物理媒体;采用随机数(这里我们叫做盐)杂凑多重加密等方法保证数据的安全性。是一种简便的加密方式。 通过 java代码实现如下:Coder 类见 单向加密 J
2、ava代码 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. * autho
3、r 梁栋 14. * version 1.0 15. * since 1.0 16. */ 17.public abstract class PBECoder extends Coder 18. /* 19. * 支持以下任意一种算法 20. * 21. * 22. * PBEWithMD5AndDES 23. * PBEWithMD5AndTripleDES 24. * PBEWithSHA1AndDESede 25. * PBEWithSHA1AndRC2_40 26. * 27. */ 28. public static final String ALGORITHM = “PBEWITH
4、MD5andDES“; 29. 30. /* 31. * 盐初始化 32. * 33. * return 34. * throws Exception 35. */ 36. public static byte initSalt() throws Exception 37. byte salt = new byte8; 38. Random random = new Random(); 39. random.nextBytes(salt); 40. return salt; 41. 42. 43. /* 44. * 转换密钥 45. * 46. * param password 47. * r
5、eturn 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);
6、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(passw
7、ord); 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 8
8、9. * 密码 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
9、.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
10、 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 dat
11、a = 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