Java簡(jiǎn)單實(shí)現(xiàn)
public class AESUtil {
private static final String KEY_ALGORITHM = "AES";
/**
* 默認(rèn)的加密算法
*/
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
/**
* 隨機(jī)生成密鑰
*
* @return
*/
public static String getAESRandomKey() {
SecureRandom random = new SecureRandom();
long randomKey = random.nextLong();
return String.valueOf(randomKey);
}
/**
* AES 加密操作
*
* @param content 待加密內(nèi)容
* @param key 加密密鑰
* @return 返回Base64轉(zhuǎn)碼后的加密數(shù)據(jù)
*/
public static String encrypt(String content, String key) throws Exception {
try {
// 創(chuàng)建密碼器
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);
//通過(guò)Base64轉(zhuǎn)碼返回
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 {
//實(shí)例化
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//使用密鑰初始化,設(shè)置為解密模式
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
//執(zhí)行操作
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 對(duì)象
try {
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
// 此類提供加密的強(qiáng)隨機(jī)數(shù)生成器 (RNG),該實(shí)現(xiàn)在windows上每次生成的key都相同,但是在部分linux或solaris系統(tǒng)上則不同。
// SecureRandom random = new SecureRandom(key.getBytes());
// 指定算法名稱,不同的系統(tǒng)上生成的key是相同的。
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
//AES 要求密鑰長(zhǎng)度為 128
kg.init(128, random);
//生成一個(gè)密鑰
SecretKey secretKey = kg.generateKey();
// 轉(zhuǎn)換為AES專用密鑰
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
} catch (NoSuchAlgorithmException ex) {
log.info("生成加密秘鑰異常!", ex);
}
return null;
}
/**
* 字節(jié)數(shù)組轉(zhuǎn)Base64編碼
*
* @param bytes
* @return
*/
public static String byte2Base64(byte[] bytes) {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(bytes);
}
/**
* Base64編碼轉(zhuǎn)字節(jié)數(shù)組
*
* @param base64Key
* @return
* @throws IOException
*/
public static byte[] base642Byte(String base64Key) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(base64Key);
}
}
測(cè)試
@Test
public void aseTest() throws Exception {
String content = "hello,您好";
String key = "sde@5f98H*^hsff%dfs$r344&df8543*er";
System.out.println("原文=" + content);
String s1 = AESUtil.encrypt(content, key);
System.out.println("加密結(jié)果=" + s1);
System.out.println("解密結(jié)果="+AESUtil.decrypt(s1, key));
}
結(jié)果:
原文=hello,您好
加密結(jié)果=zcMUh2txC6JxbGqKQeZ/kA==
解密結(jié)果=hello,您好
常見(jiàn)問(wèn)題:
1、Linux下運(yùn)行下報(bào)javax.crypto.BadPaddingException:Given final block not properly padded
原因:SecureRandom 實(shí)現(xiàn)完全隨操作系統(tǒng)本身的內(nèi)部狀態(tài),該實(shí)現(xiàn)在 windows 上每次生成的 key 都相同,但是在部分 linux 或solaris系統(tǒng)上則不同。除非調(diào)用方在調(diào)用 getInstance 方法,然后調(diào)用 setSeed方法。
方法:
// SecureRandom強(qiáng)隨機(jī)數(shù)生成器,完全隨操作系統(tǒng)本身的內(nèi)部狀態(tài)
SecureRandom random = new SecureRandom(key.getBytes());
// 改成:指定算法名稱
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
Java-加密篇
Java加解-RSA加解密
Java加密-Signature數(shù)據(jù)簽名
Java加密-AES加解密
Java加密-密鑰的保存與獲取