1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package com.citygis.utils;
- import cn.hutool.core.util.HexUtil;
- import cn.hutool.crypto.SmUtil;
- import cn.hutool.crypto.symmetric.SM4;
- import javax.crypto.SecretKey;
- /**
- * @Author: zyl
- * @CreateTime: 2024-12-13
- * @Description:
- * @Version: 1.0
- */
- public class Sm4Util {
- /**
- * 获取sm4秘钥
- *
- * @return sm4秘钥
- */
- public static String getSm4Key() {
- SM4 sm4 = SmUtil.sm4();
- SecretKey secretKey = sm4.getSecretKey();
- byte[] encoded = secretKey.getEncoded();
- return HexUtil.encodeHexStr(encoded);
- }
- /**
- * sm4加密
- *
- * @param content 文本
- * @param sm4Key sm4秘钥
- * @return 加密后的结果
- */
- public static String encrypt(String content, String sm4Key) {
- SM4 sm4 = new SM4(HexUtil.decodeHex(sm4Key));
- return sm4.encryptHex(content);
- }
- /**
- * @param content
- * @param sm4Key
- * @return 解密后的结果
- */
- public static String decrypt(String content, String sm4Key) {
- SM4 sm4 = new SM4(HexUtil.decodeHex(sm4Key));
- return sm4.decryptStr(content);
- }
- }
|