import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
/**
-
@ClassName AES
-
@Description TODO
-
@Author lyq
-
@Date 2020/2/12 9:33 下午
-
@Version 1.0
*/
public class AES {
public static final String ENCRYPT_MODE_CBC = “CBC”;
public static final String ENCRYPT_MODE_ECB = “ECB”;
/**
- 默认加密类型
*/
private static final String _ENCRYPT_MODE = “CBC”;
/**
-
AES加密
-
@param plaintext 明文
-
@param key 密钥
-
@return 该字符串的AES密文值
*/
public static String encrypt(String plaintext, String key) {
return encrypt(plaintext,key,_ENCRYPT_MODE);
}
/**
-
AES解密
-
@param cipertext 密文
-
@param key 密钥
-
@return 该密文的明文
*/
public static String decrypt(String cipertext, String key) {
return decrypt(cipertext,key,_ENCRYPT_MODE);
}
/**
-
AES加密
-
@param plainText 明文
-
@param key 密钥
-
@param encryptMode AES加密模式,CBC或ECB
-
@return 该字符串的AES密文值
*/
public static String encrypt(String plainText, String key,String encryptMode) {
if (StringUtils.isEmpty(key)|| StringUtils.isEmpty(plainText)) {
return null;
}
try {
key = getMD5(key);
byte[] raw = key.getBytes(“utf-8”);
SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”);
Cipher cipher = Cipher.getInstance(“AES/”+encryptMode+“/PKCS5Padding”);
if(encryptMode==AES.ENCRYPT_MODE_ECB) {
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
}else {
IvParameterSpec iv = new IvParameterSpec(key.getBytes(“utf-8”));//使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
}
byte[] encrypted = cipher.doFinal(plainText.getBytes(“utf-8”));
String encryptedStr=new String(new BASE64Encoder().encode(encrypted));
//此处使用BASE64做转码功能,同时能起到2次加密的作用。
return encryptedStr;
//return new String(encrypted);
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
/**
-
AES解密
-
@param cipherText 密文
-
@param key 密钥
-
@param encryptMode AES加密模式,CBC或ECB
-
@return 该密文的明文
*/
public static String decrypt(String cipherText, String key,String encryptMode) {
if (StringUtils.isEmpty(key)|| StringUtils.isEmpty(cipherText)) {
return null;
}
try {
key=getMD5(key);
byte[] raw = key.getBytes(“utf-8”);
SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”);
Cipher cipher=Cipher.getInstance(“AES/”+encryptMode+“/PKCS5Padding”);
if(encryptMode==AES.ENCRYPT_MODE_ECB) {
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
}else {
//使用CBC模式,需要一个向量iv,可增加加密算法的强度
IvParameterSpec iv = new IvParameterSpec(key.getBytes(“utf-8”));
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
}
//先用base64解密
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(cipherText);
//byte[] encrypted1 = CipherText.getBytes();
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original,“utf-8”);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
/**
-
进行MD5加密
-
@param s 要进行MD5转换的字符串
-
@return 该字符串的MD5值的8-24位
*/
public static String getMD5(String s){
char hexDigits[]={‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘A’,‘B’,‘C’,‘D’,‘E’,‘F’};
try {