报错详情:
Exception in thread "SpringThread-pool-pzj-5" Exception in thread "SpringThread-pool-pzj-10" java.lang.ExceptionInInitializerError
at com.soc.cloud.iot.schedule.log.LogTaskContent.lambda$null$10(LogTaskContent.java:321)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: cn.hutool.crypto.CryptoException: NoSuchAlgorithmException: No such algorithm: SM4/ECB/PKCS5Padding
at cn.hutool.crypto.SecureUtil.createCipher(SecureUtil.java:987)
at cn.hutool.crypto.symmetric.SymmetricCrypto.init(SymmetricCrypto.java:154)
at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:124)
at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:112)
at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:101)
at com.soc.cloud.util.SM4Helper2.<clinit>(SM4Helper2.java:10)
... 4 more
Caused by: java.security.NoSuchAlgorithmException: No such algorithm: SM4/ECB/PKCS5Padding
at javax.crypto.Cipher.getInstance(Cipher.java:687)
at cn.hutool.crypto.SecureUtil.createCipher(SecureUtil.java:985)
... 9 more
Exception in thread "SpringThread-pool-pzj-6" Exception in thread "SpringThread-pool-pzj-4" Exception in thread "SpringThread-pool-pzj-2" java.lang.NoClassDefFoundError: Could not initialize class com.soc.cloud.util.SM4Helper2
at com.soc.cloud.iot.schedule.log.LogTaskContent.lambda$null$10(LogTaskContent.java:321)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.lang.NoClassDefFoundError: Could not initialize class com.soc.cloud.util.SM4Helper2
at com.soc.cloud.iot.schedule.log.LogTaskContent.lambda$null$10(LogTaskContent.java:321)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.lang.NoClassDefFoundError: Could not initialize class com.soc.cloud.util.SM4Helper2
at com.soc.cloud.iot.schedule.log.LogTaskContent.lambda$null$10(LogTaskContent.java:321)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.lang.NoClassDefFoundError: Could not initialize class com.soc.cloud.util.SM4Helper2
at com.soc.cloud.iot.schedule.log.LogTaskContent.lambda$null$10(LogTaskContent.java:321)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
源码:文章来源:https://www.toymoban.com/news/detail-565252.html
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
public class SM4Helper2 {
//key必须是16字节,即128位
final static String key = "8464288604b7fc85";
//指明加密算法和秘钥
static SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes());
// static SymmetricCrypto sm4 = SmUtil.sm4(key.getBytes());
//加密为16进制,也可以加密成base64/字节数组
public static byte[] encryptSm4(byte[] data) {
byte[] encrypt = sm4.encrypt(data);
return encrypt;
}
//解密
public static byte[] decryptSm4(byte[] data) {
byte[] decrypt = sm4.decrypt(data);
return decrypt;
}
}
解决办法:
maven应用的包版本不对:
bc包需要1.69版本的,下面这组坐标可以解决这个问题。文章来源地址https://www.toymoban.com/news/detail-565252.html
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.5</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.69</version>
</dependency>
到了这里,关于Hutool工具进行SM4加密报错:No such algorithm: SM4/ECB/PKCS5Padding的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!