import org.bouncycastle.util.encoders.Hex;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SM4Utils {
private static String SECRET_KEY =""
private static final String IV = "";
private static final Pattern P = Pattern.compile("\\s*|\t|\r|\n");
public static String encryptData_GCM(String plainText) {
if (plainText == null) {
return null;
}
try {
SM4 sm4 = new SM4();
byte[] key;
byte[] iv;
byte[] data;
key = SM4Utils.SECRET_KEY.getBytes();
iv = SM4Utils.getIv().getBytes();
data = plainText.getBytes();
String cipherText = encrypt_crypt_gcm(key, iv, data);
if (cipherText != null && cipherText.trim().length() > 0) {
Matcher m = P.matcher(cipherText);
cipherText = m.replaceAll("");
}
return cipherText;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String encryptData_GCM(String plainText, String secretKey, String ivString) {
if (plainText == null) {
return null;
}
try {
SM4 sm4 = new SM4();
byte[] key;
byte[] iv;
byte[] data;
key = secretKey.getBytes();
iv = ivString.getBytes();
data = plainText.getBytes();
String cipherText = encrypt_crypt_gcm(key, iv, data);
if (cipherText != null && cipherText.trim().length() > 0) {
Matcher m = P.matcher(cipherText);
cipherText = m.replaceAll("");
}
return cipherText;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String encrypt_crypt_gcm(byte[] key, byte[] iv, byte[] data) throws Exception {
if (iv == null || iv.length != 16) {
throw new Exception("iv error!");
}
if (data == null) {
throw new Exception("input is null!");
}
SM4Engine engine = new SM4Engine();
GCMBlockCipher cipher = new GCMBlockCipher(engine);
KeyParameter keyParam = new KeyParameter(key);
AEADParameters params = new AEADParameters(keyParam, 128, iv, null);
cipher.init(true, params);
byte[] ciphertext = new byte[cipher.getOutputSize(data.length)];
int len = cipher.processBytes(data, 0, data.length, ciphertext, 0);
cipher.doFinal(ciphertext, len);
return Hex.toHexString(ciphertext);
}
public static String decryptData_GCM(String cipherText, String secretKey, String ivString) {
if (cipherText == null) {
return null;
}
try {
byte[] key;
byte[] iv;
byte[] data;
key = secretKey.getBytes();
iv = ivString.getBytes();
data = Hex.decode(cipherText);
return decrypt_crypt_gcm(key, iv, data);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decryptData_GCM(String cipherText) {
if (cipherText == null) {
return null;
}
try {
byte[] key;
byte[] iv;
byte[] data;
key = SECRET_KEY.getBytes();
iv = IV.getBytes();
data = Hex.decode(cipherText);
return decrypt_crypt_gcm(key, iv, data);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decrypt_crypt_gcm(byte[] key, byte[] iv, byte[] input) throws Exception {
if (input == null) {
throw new Exception("input is null!");
}
SM4Engine engine = new SM4Engine();
GCMBlockCipher cipher = new GCMBlockCipher(engine);
KeyParameter keyParam = new KeyParameter(key);
AEADParameters params = new AEADParameters(keyParam, 128, iv, null);
cipher.init(false, params);
byte[] decrypted = new byte[cipher.getOutputSize(input.length)];
int len = cipher.processBytes(input, 0, input.length, decrypted, 0);
cipher.doFinal(decrypted, len);
return new String(decrypted);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通