SM4 hutool工具类版本及非工具类版本
一、hutool工具类版本
1、jar包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
2、代码
public class SM4Utils {
public static final String SM4K = "ac02f39ca33fe4a4eab8b61dfa4e81d7";
public static String getSm4Key() {
SM4 sm4 = SmUtil.sm4();
SecretKey secretKey = sm4.getSecretKey();
byte[] encoded = secretKey.getEncoded();
return HexUtil.encodeHexStr(encoded);
}
public static String encrypt(String content) {
if (StringUtils.isBlank(content)) {
return null;
}
SM4 sm4 = new SM4(HexUtil.decodeHex(SM4K));
return sm4.encryptHex(content);
}
public static String decrypt(String content) {
if (StringUtils.isBlank(content)) {
return null;
}
SM4 sm4 = new SM4(HexUtil.decodeHex(SM4K));
return sm4.decryptStr(content);
}
public static void main(String[] args) {
String sm4Key = getSm4Key();
System.out.println("sm4秘钥:" + sm4Key);
String encrypt = encrypt("afafsgargra");
System.out.println("加密后:" + encrypt);
String decrypt = decrypt(encrypt);
System.out.println("解密后:" + decrypt);
}
}
3、运行结果

二、非工具类版本
1、jar包
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
2、代码
public class SM4Util {
static {
Security.addProvider(new BouncyCastleProvider());
}
private static final String ENCODING = "UTF-8";
public static final String ALGORITHM_NAME = "SM4";
public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
public static final int DEFAULT_KEY_SIZE = 128;
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 byte[] generateKey() throws Exception {
return generateKey(DEFAULT_KEY_SIZE);
}
public static byte[] generateKey(int keySize) throws Exception {
KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
kg.init(keySize, new SecureRandom());
return kg.generateKey().getEncoded();
}
public static String encryptEcb(String hexKey, String paramStr) throws Exception {
String cipherText = "";
byte[] keyData = ByteUtils.fromHexString(hexKey);
byte[] srcData = paramStr.getBytes(ENCODING);
byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
cipherText = ByteUtils.toHexString(cipherArray);
return cipherText;
}
public static String encryptEcbToBase64(String hexKey, String paramStr) throws Exception {
String cipherText = "";
byte[] keyData = ByteUtils.fromHexString(hexKey);
byte[] srcData = paramStr.getBytes(ENCODING);
byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
cipherText = Base64.encodeBase64String(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);
return cipher.doFinal(data);
}
public static String decryptEcb(String hexKey, String cipherText) throws Exception {
String decryptStr = "";
byte[] keyData = ByteUtils.fromHexString(hexKey);
byte[] cipherData = ByteUtils.fromHexString(cipherText);
byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);
decryptStr = new String(srcData, ENCODING);
return decryptStr;
}
public static String decryptEcb(byte[] key,byte[] encryptedData) throws Exception {
String decryptStr = "";
byte[] srcData = decrypt_Ecb_Padding(key, encryptedData);
decryptStr = new String(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);
return cipher.doFinal(cipherText);
}
public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
boolean flag = false;
byte[] keyData = ByteUtils.fromHexString(hexKey);
byte[] cipherData = ByteUtils.fromHexString(cipherText);
byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData);
byte[] srcData = paramStr.getBytes(ENCODING);
flag = Arrays.equals(decryptData, srcData);
return flag;
}
public static void main(String[] args) {
try {
String json = "13800138000";
String key = "86C63180C2806ED1F47B859DE501215B";
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);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、运行结果
