当前位置:   article > 正文

Hutool工具进行SM4加密报错:No such algorithm: SM4/ECB/PKCS5Padding_cn.hutool.crypto.cryptoexception: nosuchalgorithme

cn.hutool.crypto.cryptoexception: nosuchalgorithmexception: no such algorith

报错详情:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

源码:

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;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

解决办法:
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/560527
推荐阅读
相关标签
  

闽ICP备14008679号