aes是什么?
aes是对称加密的一种,什么是对称加密呢?就是加密和解密使用相同的秘钥的加密算法,显而易见,aes加密和md5不同,aes是可逆的,aes加密算法用于替代以前的des加密算法。
使用场景:
工作的过程中,我们有些文件因为需要携带出公司,可能需要加密,最常见的比如源代码加密。如果我想让加密可逆,又能自己指定加密和解密使用的秘钥,那么aes加密算法真的是一个不错的选择哦。因为既然可以自己指定秘钥,我们控制起来就会让aes这个加密算法安全很多。
1.工具类:
public class AESUtil {
private static final String KEY_ALGORITHM = "AES";
/**
* 默认的加密算法
*/
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
private static Logger log;
/**
* 随机生成密钥
*
* @return
*/
public static String getAESRandomKey() {
SecureRandom random = new SecureRandom();
long randomKey = random.nextLong();
return String.valueOf(randomKey);
}
/**
* AES 加密操作
*
* @param content 待加密内容
* @param key 加密密钥
* @return 返回Base64转码后的加密数据
*/
public static String encrypt(String content, String key) throws Exception {
try {
// 创建密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
byte[] byteContent = content.getBytes("utf-8");
// 初始化为加密模式的密码器
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
// 加密
byte[] result = cipher.doFinal(byteContent);
//通过Base64转码返回
return byte2Base64(result);
} catch (Exception ex) {
log.error("加密失败", ex);
}
return null;
}
/**
* AES 解密操作
*
* @param content
* @param key
* @return
*/
public static String decrypt(String content, String key) {
try {
//实例化
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
//执行操作
byte[] result = cipher.doFinal(base642Byte(content));
return new String(result, "utf-8");
} catch (Exception ex) {
log.error("解密失败", ex);
}
return null;
}
/**
* 生成加密秘钥
*
* @return
*/
private static SecretKeySpec getSecretKey(final String key) {
//返回生成指定算法密钥生成器的 KeyGenerator 对象
try {
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
// 此类提供加密的强随机数生成器 (RNG),该实现在windows上每次生成的key都相同,但是在部分linux或solaris系统上则不同。
// SecureRandom random = new SecureRandom(key.getBytes());
// 指定算法名称,不同的系统上生成的key是相同的。
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
//AES 要求密钥长度为 128
kg.init(128, random);
//生成一个密钥
SecretKey secretKey = kg.generateKey();
// 转换为AES专用密钥
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
} catch (NoSuchAlgorithmException ex) {
log.info("生成加密秘钥异常!");
}
return null;
}
/**
* 字节数组转Base64编码
*
* @param bytes
* @return
*/
public static String byte2Base64(byte[] bytes) {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(bytes);
}
/**
* Base64编码转字节数组
*
* @param base64Key
* @return
* @throws IOException
*/
public static byte[] base642Byte(String base64Key) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(base64Key);
}
}
2.测试类
public class AESUtilTest {
public static void main(String[] args) throws Exception {
String content = "hello,您好";
String key = "98496561648451AZAHVGHXQVCXQy";
System.out.println("原文=" + content);
String s1 = AESUtil.encrypt(content, key);
System.out.println("加密结果=" + s1);
System.out.println("解密结果="+AESUtil.decrypt(s1, key));
}
}
参考博客:https://www.jianshu.com/p/7f17792609cc
https://www.cnblogs.com/areful/p/10348809.html