Java SM4加密解密
短信预约 -IT技能 免费直播动态提醒
依赖
org.bouncycastle bcprov-jdk15on 1.59
SM4工具类
public class Sm4Util { static { Security.addProvider(new BouncyCastleProvider()); } private static final String ENCODING = "UTF-8"; public static final String ALGORITHM_NAME = "SM4"; // 加密算法/分组加密模式/分组填充方式 // PKCS5Padding-以8个字节为一组进行分组加密 // 定义分组加密模式使用:PKCS5Padding public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding"; public static String encryptEcb(String hexKey, String paramStr) throws Exception { String cipherText = ""; // 16进制字符串-->byte[] byte[] keyData = ByteUtils.fromHexString(hexKey); // String-->byte[] byte[] class="lazy" data-srcData = paramStr.getBytes(ENCODING); // 加密后的数组 byte[] cipherArray = encrypt_Ecb_Padding(keyData, class="lazy" data-srcData); // byte[]-->hexString cipherText = ByteUtils.toHexString(cipherArray); return cipherText; } public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception { Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);//声称Ecb暗号,通过第二个参数判断加密还是解密 return cipher.doFinal(data); } private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception { Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME); Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME); cipher.init(mode, sm4Key); return cipher; } //解密**************************************** public static String decryptEcb(String hexKey, String cipherText) throws Exception { // 用于接收解密后的字符串 String decryptStr = ""; // hexString-->byte[] byte[] keyData = ByteUtils.fromHexString(hexKey); // hexString-->byte[] byte[] cipherData = ByteUtils.fromHexString(cipherText); // 解密 byte[] class="lazy" data-srcData = decrypt_Ecb_Padding(keyData, cipherData); // byte[]-->String decryptStr = new String(class="lazy" data-srcData, ENCODING); return decryptStr; } public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception { Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);//生成Ecb暗号,通过第二个参数判断加密还是解密 return cipher.doFinal(cipherText); } public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception { // 用于接收校验结果 boolean flag = false; // hexString-->byte[] byte[] keyData = ByteUtils.fromHexString(hexKey); // 将16进制字符串转换成数组 byte[] cipherData = ByteUtils.fromHexString(cipherText); // 解密 byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData); // 将原字符串转换成byte[] byte[] class="lazy" data-srcData = paramStr.getBytes(ENCODING); // 判断2个数组是否一致 flag = Arrays.equals(decryptData, class="lazy" data-srcData); return flag; }}
测试
public static void main(String[] args) { try { System.out.println("开始测试SM4加密解密===================="); String json = "{ keubgibzpcax: { dvwvgxsfcq: lvCAhMFcXoK9YB1B},dongubnfea: D9fpTrZOzByv}"; System.out.println("加密前:" + json); //自定义的32位16进制秘钥 String key = "E99567ACC5364D69BA0B0BC83066955F"; //sm4加密 String cipher = Sm4Util.encryptEcb(key, json); System.out.println("加密后:" + cipher); //校验加密前后是否为同一数据 System.out.println("校验:" + Sm4Util.verifyEcb(key, cipher, json)); //解密 json = Sm4Util.decryptEcb(key, cipher); System.out.println("解密后:" + json); System.out.println("结束==================="); } catch (Exception e) { e.printStackTrace(); } }
来源地址:https://blog.csdn.net/weixin_50272584/article/details/130620339
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341