Hutool工具进行SM4加密报错:No such algorithm: SM4/ECB/PKCS5Padding

报错详情:

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)

源码:

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版本的,下面这组坐标可以解决这个问题。

        <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>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值