当前位置:   article > 正文

java SM4加密报错 No such algorithm: SM4/ECB/PKCS5Padding

no such algorithm: sm4
  1. import org.apache.commons.codec.binary.Hex;
  2. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  3. import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.KeyGenerator;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import java.security.Key;
  8. import java.security.SecureRandom;
  9. import java.security.Security;
  10. import java.util.Arrays;
  11. public class SM4Util {
  12. static {
  13. Security.addProvider(new BouncyCastleProvider());
  14. }
  15. private static final String ENCODING = "UTF-8";
  16. public static final String ALGORITHM_NAME = "SM4";
  17. // 加密算法/分组加密模式/分组填充方式
  18. // PKCS5Padding-以8个字节为一组进行分组加密
  19. // 定义分组加密模式使用:PKCS5Padding
  20. public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
  21. // 128-32位16进制;256-64位16进制
  22. public static final int DEFAULT_KEY_SIZE = 128;
  23. /**
  24. * 自动生成密钥
  25. *
  26. * @return
  27. * @explain
  28. */
  29. public static String generateKey() throws Exception {
  30. return new String(Hex.encodeHex(generateKey(DEFAULT_KEY_SIZE),false));
  31. }
  32. /**
  33. * @param keySize
  34. * @return
  35. * @throws Exception
  36. * @explain
  37. */
  38. public static byte[] generateKey(int keySize) throws Exception {
  39. KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
  40. kg.init(keySize, new SecureRandom());
  41. return kg.generateKey().getEncoded();
  42. }
  43. /**
  44. * 生成ECB暗号
  45. *
  46. * @param algorithmName 算法名称
  47. * @param mode 模式
  48. * @param key
  49. * @return
  50. * @throws Exception
  51. * @explain ECB模式(电子密码本模式:Electronic codebook)
  52. */
  53. private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
  54. Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
  55. Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
  56. cipher.init(mode, sm4Key);
  57. return cipher;
  58. }
  59. /**
  60. * sm4加密
  61. *
  62. * @param hexKey 16进制密钥(忽略大小写)
  63. * @param paramStr 待加密字符串
  64. * @return 返回16进制的加密字符串
  65. * @explain 加密模式:ECB
  66. * 密文长度不固定,会随着被加密字符串长度的变化而变化
  67. */
  68. public static String encryptEcb(String hexKey, String paramStr) {
  69. try {
  70. String cipherText = "";
  71. // 16进制字符串-->byte[]
  72. byte[] keyData = ByteUtils.fromHexString(hexKey);
  73. // String-->byte[]
  74. byte[] srcData = paramStr.getBytes(ENCODING);
  75. // 加密后的数组
  76. byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
  77. // byte[]-->hexString
  78. cipherText = ByteUtils.toHexString(cipherArray);
  79. return cipherText;
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. return paramStr;
  83. }
  84. }
  85. /**
  86. * 加密模式之Ecb
  87. *
  88. * @param key
  89. * @param data
  90. * @return
  91. * @throws Exception
  92. * @explain
  93. */
  94. public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
  95. Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
  96. return cipher.doFinal(data);
  97. }
  98. /**
  99. * sm4解密
  100. *
  101. * @param hexKey 16进制密钥
  102. * @param cipherText 16进制的加密字符串(忽略大小写)
  103. * @return 解密后的字符串
  104. * @throws Exception
  105. * @explain 解密模式:采用ECB
  106. */
  107. public static String decryptEcb(String hexKey, String cipherText) {
  108. // 用于接收解密后的字符串
  109. String decryptStr = "";
  110. // hexString-->byte[]
  111. byte[] keyData = ByteUtils.fromHexString(hexKey);
  112. // hexString-->byte[]
  113. byte[] cipherData = ByteUtils.fromHexString(cipherText);
  114. // 解密
  115. byte[] srcData = new byte[0];
  116. try {
  117. srcData = decrypt_Ecb_Padding(keyData, cipherData);
  118. // byte[]-->String
  119. decryptStr = new String(srcData, ENCODING);
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. return decryptStr;
  124. }
  125. /**
  126. * 解密
  127. *
  128. * @param key
  129. * @param cipherText
  130. * @return
  131. * @throws Exception
  132. * @explain
  133. */
  134. public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
  135. Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
  136. return cipher.doFinal(cipherText);
  137. }
  138. /**
  139. * 校验加密前后的字符串是否为同一数据
  140. *
  141. * @param hexKey 16进制密钥(忽略大小写)
  142. * @param cipherText 16进制加密后的字符串
  143. * @param paramStr 加密前的字符串
  144. * @return 是否为同一数据
  145. * @throws Exception
  146. * @explain
  147. */
  148. public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
  149. // 用于接收校验结果
  150. boolean flag = false;
  151. // hexString-->byte[]
  152. byte[] keyData = ByteUtils.fromHexString(hexKey);
  153. // 将16进制字符串转换成数组
  154. byte[] cipherData = ByteUtils.fromHexString(cipherText);
  155. // 解密
  156. byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData);
  157. // 将原字符串转换成byte[]
  158. byte[] srcData = paramStr.getBytes(ENCODING);
  159. // 判断2个数组是否一致
  160. flag = Arrays.equals(decryptData, srcData);
  161. return flag;
  162. }
  163. }

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

本地不报错  上线后报错

往linux上的jdk文件夹  /jre/lib/ext 下加入bcprov-jdk15on 的jar包即可  每个版本都试一下

把jar包版本换一下也可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/560530
推荐阅读
相关标签
  

闽ICP备14008679号