package ***.utils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
public class AESUtil {
private static final String ALGORITHM = "AES";
private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";
public static String generateDesKey() throws NoSuchAlgorithmException {
KeyGenerator kgen = null;
kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey skey = kgen.generateKey();
return BytesUtil.bytes2Hex(skey.getEncoded());
}
public static String encrypt2Base64(String data, String hexSecret) throws Exception {
byte[] encrypt = encrypt(data, hexSecret);
return Base64Util.encodeToString(encrypt);
}
public static String encrypt2Base64(byte[] data, String hexSecret) throws Exception {
byte[] encrypt = encrypt(data, hexSecret);
return Base64Util.encodeToString(encrypt);
}
public static byte[] encrypt(String data, String hexSecret) throws Exception {
return encrypt(data.getBytes(), hexSecret);
}
public static byte[] encrypt(byte[] data, String hexSecret) throws Exception {
if (hexSecret.length() != 32) {
throw new IllegalArgumentException("Hex secret's length must be 32");
}
return encrypt(data, BytesUtil.hex2Bytes(hexSecret));
}
public static String encrypt2Base64Safe(String data, String hexSecret) {
byte[] encrypt = new byte[0];
try {
encrypt = encrypt(data, hexSecret);
} catch (Exception e) {
e.printStackTrace();
}
return Base64Util.encodeToString(encrypt);
}
public static String encrypt2Base64Safe(byte[] data, String hexSecret) {
byte[] encrypt = new byte[0];
try {
encrypt = encrypt(data, hexSecret);
} catch (Exception e) {
e.printStackTrace();
}
return Base64Util.encodeToString(encrypt);
}
public static byte[] encryptSafe(String data, String hexSecret) {
try {
return encrypt(data.getBytes(), hexSecret);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] encryptSafe(byte[] data, String hexSecret) {
if (hexSecret.length() != 32) {
throw new IllegalArgumentException("Hex secret's length must be 32");
}
try {
return encrypt(data, BytesUtil.hex2Bytes(hexSecret));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] encrypt(byte[] data, byte[] secret) throws Exception {
if (secret == null || secret.length != 16) {
throw new IllegalArgumentException("secret's length must be 16");
}
SecretKeySpec key = new SecretKeySpec(secret, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
public static String decrypt2String(String base64Data, String hexSecret) throws Exception {
byte[] decrypt = decrypt(base64Data, hexSecret);
return new String(decrypt);
}
public static byte[] decrypt(String base64Data, String hexSecret) throws Exception {
return decrypt(Base64Util.decode(base64Data), BytesUtil.hex2Bytes(hexSecret));
}
public static byte[] decrypt(byte[] data, String hexSecret) throws Exception {
return decrypt(data, BytesUtil.hex2Bytes(hexSecret));
}
public static byte[] decrypt(byte[] data, byte[] secret) throws Exception {
if (secret == null || secret.length != 16) {
throw new IllegalArgumentException("secret's length must be 16");
}
SecretKeySpec key = new SecretKeySpec(secret, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
}
}