基于Java的一个AES加密解密工具类
package gov.pro.data.common.utils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Scanner;
public class CipherUtils {
public static final String secretKey = "MIICdQIBADANBgkqhkiG9";
public static final String AES = "AES";
public static final String charsetName = "UTF-8";
public static SecretKeySpec generateKey(String password) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
keyGenerator.init(128, random);
SecretKey original_key = keyGenerator.generateKey();
SecretKeySpec secretKeySpec = new SecretKeySpec(original_key.getEncoded(), AES);
return secretKeySpec;
}
public static String AESEncode(String content, String password) {
try {
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE, generateKey(password));
byte[] bytes = cipher.doFinal(content.getBytes(charsetName));
return Base64.getUrlEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String AESDecode(String content, String password) {
try {
byte[] bytes = Base64.getUrlDecoder().decode(content);
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, generateKey(password));
byte[] result = cipher.doFinal(bytes);
return new String(result, charsetName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String encrypt(String content) {
return AESEncode(content, secretKey);
}
public static String decrypt(String content) {
return AESDecode(content, secretKey);
}
}
|