import cn.hutool.crypto.symmetric.SymmetricCrypto;
/**
* @author : chenfan
* @className : SM4Utils
* @date : Created in 2022/11/3 13:55
* @description :國密算法SM4加解密工具類
*/
/**
* 國密SM4分組密碼算法工具類(對稱加密)
*/
public class SM4Utils {
//SM4-加密
public static String encryptSm4(String plaintext, String key) {
//指明加密算法和秘鑰
SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes());
return sm4.encryptHex(plaintext);
}
//SM4-解密
public static String decryptSm4(String ciphertext, String key) {
//指明加密算法和秘鑰
SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes());
return sm4.decryptStr(ciphertext);
}
public static void main(String[] args) {
String content = "Hello SM4 國密算法";
String key = "pGmo2jZkrR8JqlQn";
String plain = encryptSm4(content, key);
String cipher = decryptSm4(plain, key);
System.out.println(plain + "\n" + cipher);
}
}
運行結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-506515.html
文章來源:http://www.zghlxwxcb.cn/news/detail-506515.html
到了這里,關(guān)于國密算法-SM4加解密工具類的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!