AES加密和解密工具类
pom
<!-- lombok 用于Slf4j日志注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
工具类
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.ztsk.framework.commons.config.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class AESUtil {
private static Cipher dbCipher;
private static Cipher webCipher;
private static final String KEY_CHARSET = "UTF-8";
private static final String AES = "AES";
private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
private static final String CIPHER_ALGORITHM_GCM = "AES/GCM/NoPadding";
private static final Integer PRIVATE_KEY_SIZE_BIT = 128;
private static final Integer PRIVATE_KEY_SIZE_BYTE = 16;
public static String encrypt(String plainText,String secretKey) {
if (StringUtils.isBlank(plainText)){
return plainText;
}
if (secretKey.length() != PRIVATE_KEY_SIZE_BYTE) {
throw new RuntimeException("AESUtil:Invalid AES secretKey length (must be 16 bytes)");
}
String cipherText = "";
try {
initParam(secretKey, Cipher.ENCRYPT_MODE,CIPHER_ALGORITHM_GCM);
byte[] bytePlainText = plainText.getBytes(KEY_CHARSET);
byte[] byteCipherText = dbCipher.doFinal(bytePlainText);
cipherText = Base64.encodeBase64String(byteCipherText);
} catch (Exception e) {
log.error("AESUtil:encrypt fail!:" + e.getMessage());
throw new BaseException("AESUtil:encrypt fail!", e);
}
return cipherText;
}
public static String decrypt(String cipherText,String secretKey) {
if (StringUtils.isBlank(cipherText)){
return cipherText;
}
if (secretKey.length() != PRIVATE_KEY_SIZE_BYTE) {
throw new RuntimeException("AESUtil:Invalid AES secretKey length (must be 16 bytes)");
}
String plainText = "";
try {
initParam(secretKey, Cipher.DECRYPT_MODE,CIPHER_ALGORITHM_GCM);
byte[] byteCipherText = Base64.decodeBase64(cipherText);
if (byteCipherText.length == 0){
return cipherText;
}
byte[] bytePlainText = dbCipher.doFinal(byteCipherText);
plainText = new String(bytePlainText, KEY_CHARSET);
} catch (Exception e) {
log.info("AESUtil:decrypt fail!:"+cipherText);
return cipherText;
}
return plainText;
}
public static boolean isCiphertext(String cipherText,String secretKey) {
if (StringUtils.isBlank(cipherText)){
return false;
}
if (secretKey.length() != PRIVATE_KEY_SIZE_BYTE) {
throw new RuntimeException("AESUtil:Invalid AES secretKey length (must be 16 bytes)");
}
String plainText = "";
try {
initParam(secretKey, Cipher.DECRYPT_MODE,CIPHER_ALGORITHM_GCM);
byte[] byteCipherText = Base64.decodeBase64(cipherText);
if (byteCipherText.length == 0){
return false;
}
byte[] bytePlainText = dbCipher.doFinal(byteCipherText);
plainText = new String(bytePlainText, KEY_CHARSET);
} catch (Exception e) {
log.info("AESUtil:decrypt fail!:"+cipherText);
return false;
}
return true;
}
public static String encryptCBC(String plainText,String secretKey) {
if (StringUtils.isBlank(plainText)){
return plainText;
}
if (secretKey.length() != PRIVATE_KEY_SIZE_BYTE) {
throw new RuntimeException("AESUtil:Invalid AES secretKey length (must be 16 bytes)");
}
String cipherText = "";
try {
initParam(secretKey, Cipher.ENCRYPT_MODE,CIPHER_ALGORITHM_CBC);
byte[] bytePlainText = plainText.getBytes(KEY_CHARSET);
byte[] byteCipherText = webCipher.doFinal(bytePlainText);
cipherText = Base64.encodeBase64String(byteCipherText);
} catch (Exception e) {
throw new RuntimeException("AESUtil:encrypt fail!", e);
}
return cipherText;
}
public static String decryptCBC(String cipherText,String secretKey) {
if (StringUtils.isBlank(cipherText)){
return cipherText;
}
if (secretKey.length() != PRIVATE_KEY_SIZE_BYTE) {
throw new RuntimeException("AESUtil:Invalid AES secretKey length (must be 16 bytes)");
}
String plainText = "";
try {
initParam(secretKey, Cipher.DECRYPT_MODE,CIPHER_ALGORITHM_CBC);
byte[] byteCipherText = Base64.decodeBase64(cipherText);
if (byteCipherText.length == 0){
return cipherText;
}
byte[] bytePlainText = webCipher.doFinal(byteCipherText);
plainText = new String(bytePlainText, KEY_CHARSET);
} catch (Exception e) {
log.info("AESUtil:decrypt fail!:"+cipherText);
return cipherText;
}
return plainText;
}
public static void initParam(String secretKey, int mode,String cipherAlgorithm) {
try {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secretKey.getBytes());
KeyGenerator keygen = KeyGenerator.getInstance(AES);
keygen.init(PRIVATE_KEY_SIZE_BIT, secureRandom);
byte[] raw = secretKey.getBytes();
SecretKeySpec key = new SecretKeySpec(raw, AES);
if (CIPHER_ALGORITHM_CBC.equals(cipherAlgorithm)){
webCipher = Cipher.getInstance(cipherAlgorithm);
IvParameterSpec iv = new IvParameterSpec(secretKey.getBytes());
webCipher.init(mode, key, iv);
}else if (CIPHER_ALGORITHM_GCM.equals(cipherAlgorithm)){
dbCipher = Cipher.getInstance(cipherAlgorithm);
GCMParameterSpec iv = new GCMParameterSpec(PRIVATE_KEY_SIZE_BIT,secretKey.getBytes());
dbCipher.init(mode, key, iv);
}
} catch (Exception e) {
throw new RuntimeException("AESUtil:initParam fail!", e);
}
}
public static void main(String[] args) {
String s1 = AESUtil.encryptCBC("王", "qwertyuiopqwert8");
String s = AESUtil.decryptCBC(s1, "qwertyuiopqwert8");
System.out.println(s);
System.out.println(s1);
}
}