package wst.st.site.tools;import java.security.Key;import java.security.SecureRandom;import java.security.Security;import java.util.Arrays;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.spec.SecretKeySpec;import org.bouncycastle.jce.provider.BouncyCastleProvider;import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;/** * SM4加解密工具 * @author wst 2023年8月22日 下午5:31:36 * */public class SM4Util { // 编码 private static final String ENCODING = "UTF-8"; // 加密名称 public static final String ALGORIGTHM_NAME = "SM4"; // 加密分组方式 public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS7Padding"; // KEY长度 public static final int DEFAULT_KEY_SIZE = 128; public SM4Util() { } static { Security.addProvider(new BouncyCastleProvider()); } // 生成ecb暗号 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, ALGORIGTHM_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(ALGORIGTHM_NAME, BouncyCastleProvider.PROVIDER_NAME); kg.init(keySize, new SecureRandom()); return kg.generateKey().getEncoded(); } // 加密 public static String encryptEcb(String hexKey, String paramStr, String charset) throws Exception { String cipherText = ""; if (null != paramStr && !"".equals(paramStr)) { byte[] keyData = ByteUtils.fromHexString(hexKey); charset = charset.trim(); if (charset.length() <= 0) { charset = ENCODING; } byte[] srcData = paramStr.getBytes(charset); byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData); cipherText = ByteUtils.toHexString(cipherArray); } return cipherText; } // 加密模式之ecb public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception { Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key); byte[] bs = cipher.doFinal(data); return bs; } // Sm4解密 public static String decryptEcb(String hexKey, String cipherText, String charset) throws Exception { String decryptStr = ""; byte[] keyData = ByteUtils.fromHexString(hexKey); byte[] cipherData = ByteUtils.fromHexString(cipherText); byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData); charset = charset.trim(); if (charset.length() <= 0) { charset = ENCODING; } decryptStr = new String(srcData, charset); 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 = "{\"name\":\"Devil\"}"; System.out.println("json: " + json); String key = ByteUtils.toHexString(SM4Util.generateKey()); System.out.println("key: " + key); String encryptJson = SM4Util.encryptEcb(key, json, ENCODING); System.out.println("encryptJson: " + encryptJson); System.out.println(SM4Util.verifyEcb(key, encryptJson, json)); String decryptJson = SM4Util.decryptEcb(key, encryptJson, ENCODING); System.out.println("decryptJson: " + decryptJson); } catch (Exception e) { e.printStackTrace(); } }}