init
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
package com.mgm.mcrm.common.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.GCMParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.Permission;
|
||||
import java.security.PermissionCollection;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
public class AESUtil {
|
||||
|
||||
private static final String AES = "AES";
|
||||
private static final int GCM_IV_LENGTH = 12; // GCM推荐使用12字节的IV
|
||||
private static final int GCM_TAG_LENGTH = 16; // GCM认证标签长度(128位)
|
||||
|
||||
private static java.util.Base64.Encoder base64Encoder = java.util.Base64.getEncoder();
|
||||
private static java.util.Base64.Decoder base64Decoder = java.util.Base64.getDecoder();
|
||||
|
||||
static {
|
||||
java.security.Security.setProperty("crypto.policy", "unlimited");
|
||||
removeCryptographyRestrictions();
|
||||
}
|
||||
|
||||
public static void removeCryptographyRestrictions() {
|
||||
try {
|
||||
/*
|
||||
* Do the following, but with reflection to bypass access checks:
|
||||
* JceSecurity.isRestricted = false;
|
||||
* JceSecurity.defaultPolicy.perms.clear();
|
||||
* JceSecurity.defaultPolicy.add(CryptoAllPermission.INSTANCE);
|
||||
*/
|
||||
final Class<?> jceSecurity = Class.forName("javax.crypto.JceSecurity");
|
||||
final Class<?> cryptoPermissions = Class.forName("javax.crypto.CryptoPermissions");
|
||||
final Class<?> cryptoAllPermission = Class.forName("javax.crypto.CryptoAllPermission");
|
||||
|
||||
final Field isRestrictedField = jceSecurity.getDeclaredField("isRestricted");
|
||||
isRestrictedField.setAccessible(true);
|
||||
// 去除isRestricted的final限制
|
||||
final Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
modifiersField.setAccessible(true);
|
||||
modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL);
|
||||
isRestrictedField.set(null, false);
|
||||
|
||||
final Field defaultPolicyField = jceSecurity.getDeclaredField("defaultPolicy");
|
||||
defaultPolicyField.setAccessible(true);
|
||||
final PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null);
|
||||
|
||||
final Field perms = cryptoPermissions.getDeclaredField("perms");
|
||||
perms.setAccessible(true);
|
||||
((Map<?, ?>) perms.get(defaultPolicy)).clear();
|
||||
|
||||
final Field instance = cryptoAllPermission.getDeclaredField("INSTANCE");
|
||||
instance.setAccessible(true);
|
||||
defaultPolicy.add((Permission) instance.get(null));
|
||||
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static SecretKey generateKey() throws Exception {
|
||||
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
|
||||
keyGen.init(256);
|
||||
return keyGen.generateKey();
|
||||
}
|
||||
public static SecretKey stringToSecretKey(String keyString) {
|
||||
byte[] keyBytes = base64Decoder.decode(keyString);
|
||||
return new SecretKeySpec(keyBytes, AES);
|
||||
}
|
||||
public static String secretKeyToString(SecretKey secretKey) {
|
||||
byte[] keyBytes = secretKey.getEncoded();
|
||||
return base64Encoder.encodeToString(keyBytes);
|
||||
}
|
||||
/**
|
||||
* AES加密
|
||||
*/
|
||||
public static String AES256Encrypt(String key, String content) {
|
||||
try {
|
||||
SecretKey secretKey = stringToSecretKey(key);
|
||||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
byte[] iv = new byte[GCM_IV_LENGTH];
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
secureRandom.nextBytes(iv); // 生成随机的IV
|
||||
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, spec);
|
||||
|
||||
byte[] ciphertext = cipher.doFinal(content.getBytes());
|
||||
byte[] encryptedData = new byte[iv.length + ciphertext.length];
|
||||
System.arraycopy(iv, 0, encryptedData, 0, iv.length);
|
||||
System.arraycopy(ciphertext, 0, encryptedData, iv.length, ciphertext.length);
|
||||
return base64Encoder.encodeToString(encryptedData);
|
||||
|
||||
} catch (Exception e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* AES解密
|
||||
*/
|
||||
public static String AES256Decrypt(String key, String content) {
|
||||
try {
|
||||
SecretKey secretKey = stringToSecretKey(key);
|
||||
byte[] encryptedData = base64Decoder.decode(content);
|
||||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
// 从加密数据中提取IV
|
||||
byte[] iv = new byte[GCM_IV_LENGTH];
|
||||
System.arraycopy(encryptedData, 0, iv, 0, iv.length);
|
||||
|
||||
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
|
||||
|
||||
// 解密数据
|
||||
byte[] ciphertext = new byte[encryptedData.length - GCM_IV_LENGTH];
|
||||
System.arraycopy(encryptedData, GCM_IV_LENGTH, ciphertext, 0, ciphertext.length);
|
||||
byte[] byteDecode = cipher.doFinal(ciphertext);
|
||||
return new String(byteDecode, java.nio.charset.StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String sha256(String plainString) {
|
||||
String cipherString = null;
|
||||
try {
|
||||
// 获取实例(SHA-512同理)
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
|
||||
// 计算摘要
|
||||
byte[] cipherBytes = messageDigest.digest(plainString.getBytes(StandardCharsets.UTF_8));
|
||||
// 输出为16进制字符串
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : cipherBytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
cipherString = sb.toString();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return cipherString;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user