在线加密工具链接

在线SM4国密加密/解密—LZL在线工具在线SM4国密加密/解密https://lzltool.cn/SM4

根据项目选择相应数据格式

pom.xml引入一下jar包

cn.hutoolhutool-all5.8.7
org.bouncycastlebcpkix-jdk15on1.70

实现SM4加密和解密代码部分

import cn.hutool.core.util.CharsetUtil;import cn.hutool.crypto.Mode;import cn.hutool.crypto.Padding;import cn.hutool.crypto.symmetric.SM4;import java.io.IOException;public class Sm4Utils {public Sm4Utils() {}//加密为16进制,也可以加密成base64/字节数组public static String encryptSm4(String plaintext, String key, String iv) {SM4 sm4 = new SM4(Mode.CBC, Padding.PKCS5Padding, key.getBytes(), iv.getBytes());return sm4.encryptBase64(plaintext);}//解密public static String decryptSm4(String ciphertext, String key,String iv) {SM4 sm4 = new SM4(Mode.CBC, Padding.PKCS5Padding, key.getBytes(), iv.getBytes());return sm4.decryptStr(ciphertext, CharsetUtil.CHARSET_UTF_8);}public static void main(String[] args) throws IOException {String plainText = "文字内容";Sm4Utils sm4 = new Sm4Utils();String key = "输入key值";String iv= "输入iv值";System.out.println("CBC模式加密");String cipherText = sm4.encryptSm4(plainText, key,iv);System.out.println("密文: " + cipherText);System.out.println();System.out.println("CBC模式解密");String iv1= "iv值";String data= "RddoC3+TFYWB+JgxfYkBxQ6GH/iMVpgCtGMpPkZ5yCcHrK5/IS68EUWaRKQ7PBbTxy4A60SBQETlKO3jQrPvAPDShrpihOMpjIxFfobr+8rA0qoEBE5oD+SXZv4iDSANFjEMguinl1DEc0Xj9LjJ2g==";String plainText1 = sm4.decryptSm4(data, key,iv1);System.out.println("明文: " + plainText1);System.out.println();//}}