需求

java 实现 AES/CBC/PKCS7Padding 加密 解密
由于java原生只支持到PKCS5Padding
需要引入第三方依赖支持
参考:https://www.cnblogs.com/chen-lhx/p/6233954.html

实现

引入maven

  1. <dependency>
  2. <groupId>org.bouncycastle</groupId>
  3. <artifactId>bcprov-jdk15on</artifactId>
  4. <version>1.57</version>
  5. </dependency>

java工具类

  1. package test
  2. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  3. import org.bouncycastle.util.Arrays;
  4. import org.bouncycastle.util.encoders.Base64;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.security.Key;
  9. import java.security.Security;
  10. public class AESUtils {
  11. private static final String KEY_ALGORITHM = "AES";
  12. private static final String ALGORITHM_STR = "AES/CBC/PKCS7Padding";
  13. private static Key key;
  14. private static Cipher cipher;
  15. private static void init(byte[] keyBytes) {
  16. // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
  17. int base = 16;
  18. if (keyBytes.length % base != 0) {
  19. int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
  20. byte[] temp = new byte[groups * base];
  21. Arrays.fill(temp, (byte) 0);
  22. System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
  23. keyBytes = temp;
  24. }
  25. // 初始化
  26. Security.addProvider(new BouncyCastleProvider());
  27. // 转化成JAVA的密钥格式
  28. key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
  29. try {
  30. // 初始化cipher
  31. cipher = Cipher.getInstance(ALGORITHM_STR, "BC");
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. /**
  37. * 加密方法
  38. *
  39. * @param decryptedData 要加密的字符串
  40. * @param keyBytes 加密密钥
  41. * @param ivs 自定义对称解密算法初始向量
  42. * @return
  43. */
  44. public static byte[] encryptOfDiyIV(byte[] decryptedData, byte[] keyBytes, byte[] ivs) {
  45. byte[] encryptedText = null;
  46. init(keyBytes);
  47. try {
  48. cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(ivs));
  49. encryptedText = cipher.doFinal(decryptedData);
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. return encryptedText;
  54. }
  55. /**
  56. * 解密方法
  57. *
  58. * @param encryptedData 要解密的字符串
  59. * @param keyBytes 解密密钥
  60. * @param ivs 自定义对称解密算法初始向量 iv
  61. * @return 解密后的字节数组
  62. */
  63. private static byte[] decryptOfDiyIV(byte[] encryptedData, byte[] keyBytes, byte[] ivs) {
  64. byte[] encryptedText = null;
  65. init(keyBytes);
  66. try {
  67. cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivs));
  68. encryptedText = cipher.doFinal(encryptedData);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. return encryptedText;
  73. }
  74. public static String decryptData(String encryptDataB64, String sessionKeyB64, String ivB64) {
  75. return new String(
  76. decryptOfDiyIV(
  77. Base64.decode(encryptDataB64),
  78. Base64.decode(sessionKeyB64),
  79. Base64.decode(ivB64)
  80. )
  81. );
  82. }
  83. public static String encryptData(String encryptDataB64, String sessionKeyB64, String ivB64) {
  84. return new String(Base64.encode(
  85. encryptOfDiyIV(
  86. Base64.decode(encryptDataB64),
  87. Base64.decode(sessionKeyB64),
  88. Base64.decode(ivB64)
  89. )
  90. ));
  91. }
  92. public static void main(String[] args) {
  93. String iv = "1234432112344444";
  94. String key = "sadhowadaskd123sxdaj122sa12";
  95. String string = "!@#$测试1234asdvcz";
  96. System.out.println("原文:" + string);
  97. String code = encryptData(
  98. new String(Base64.encode(string.getBytes()))
  99. , new String(Base64.encode(key.getBytes()))
  100. , new String(Base64.encode(iv.getBytes()))
  101. );
  102. System.out.println("密文:" + code);
  103. String result = decryptData(
  104. code
  105. , new String(Base64.encode(key.getBytes()))
  106. , new String(Base64.encode(iv.getBytes()))
  107. );
  108. System.out.println("解密:" + result);
  109. }
  110. }

运行结果

  1. 原文:!@#$测试1234asdvcz
  2. 密文:xQzhVGWn2DSb+D1gU+X8+2+gX0d09exSebGOrGJxT2c=
  3. 解密:!@#$测试1234asdvcz