介绍对称加密的另一个算法——PBE

时间:2023-03-09 09:14:15
介绍对称加密的另一个算法——PBE

除了DES,我们还知道有DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)等多种对称加密方式,其实现方式大同小异,这里介绍对称加密的另一个算法——PBE


PBE

    PBE——Password-based encryption(基于密码加密)。其特点在于口令由用户自己掌管,不借助任何物理媒体;采用随机数(这里我们叫做盐)杂凑多重加密等方法保证数据的安全性。是一种简便的加密方式。



介绍对称加密的另一个算法——PBE



通过java代码实现如下:Coder类见 Java加密技术(一)

  1. import java.security.Key;
  2. import java.util.Random;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.SecretKey;
  5. import javax.crypto.SecretKeyFactory;
  6. import javax.crypto.spec.PBEKeySpec;
  7. import javax.crypto.spec.PBEParameterSpec;
  8. /**
  9. * PBE安全编码组件
  10. *
  11. * @author 梁栋
  12. * @version 1.0
  13. * @since 1.0
  14. */
  15. public abstract class PBECoder extends Coder {
  16. /**
  17. * 支持以下任意一种算法
  18. *
  19. * <pre>
  20. * PBEWithMD5AndDES
  21. * PBEWithMD5AndTripleDES
  22. * PBEWithSHA1AndDESede
  23. * PBEWithSHA1AndRC2_40
  24. * </pre>
  25. */
  26. public static final String ALGORITHM = "PBEWITHMD5andDES";
  27. /**
  28. * 盐初始化
  29. *
  30. * @return
  31. * @throws Exception
  32. */
  33. public static byte[] initSalt() throws Exception {
  34. byte[] salt = new byte[8];
  35. Random random = new Random();
  36. random.nextBytes(salt);
  37. return salt;
  38. }
  39. /**
  40. * 转换密钥<br>
  41. *
  42. * @param password
  43. * @return
  44. * @throws Exception
  45. */
  46. private static Key toKey(String password) throws Exception {
  47. PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
  48. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
  49. SecretKey secretKey = keyFactory.generateSecret(keySpec);
  50. return secretKey;
  51. }
  52. /**
  53. * 加密
  54. *
  55. * @param data
  56. *            数据
  57. * @param password
  58. *            密码
  59. * @param salt
  60. *            盐
  61. * @return
  62. * @throws Exception
  63. */
  64. public static byte[] encrypt(byte[] data, String password, byte[] salt)
  65. throws Exception {
  66. Key key = toKey(password);
  67. PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);
  68. Cipher cipher = Cipher.getInstance(ALGORITHM);
  69. cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  70. return cipher.doFinal(data);
  71. }
  72. /**
  73. * 解密
  74. *
  75. * @param data
  76. *            数据
  77. * @param password
  78. *            密码
  79. * @param salt
  80. *            盐
  81. * @return
  82. * @throws Exception
  83. */
  84. public static byte[] decrypt(byte[] data, String password, byte[] salt)
  85. throws Exception {
  86. Key key = toKey(password);
  87. PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);
  88. Cipher cipher = Cipher.getInstance(ALGORITHM);
  89. cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  90. return cipher.doFinal(data);
  91. }
  92. }

再给出一个测试类:

  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 PBECoderTest {
  10. @Test
  11. public void test() throws Exception {
  12. String inputStr = "abc";
  13. System.err.println("原文: " + inputStr);
  14. byte[] input = inputStr.getBytes();
  15. String pwd = "efg";
  16. System.err.println("密码: " + pwd);
  17. byte[] salt = PBECoder.initSalt();
  18. byte[] data = PBECoder.encrypt(input, pwd, salt);
  19. System.err.println("加密后: " + PBECoder.encryptBASE64(data));
  20. byte[] output = PBECoder.decrypt(data, pwd, salt);
  21. String outputStr = new String(output);
  22. System.err.println("解密后: " + outputStr);
  23. assertEquals(inputStr, outputStr);
  24. }
  25. }

控制台输出:

  1. 原文: abc
  2. 密码: efg
  3. 加密后: iCZ0uRtaAhE=
  4. 解密后: abc

后续我们会介绍非对称加密算法,如RSA、DSA、DH、ECC等。