Java实现AES算法的实例代码
短信预约 -IT技能 免费直播动态提醒
使用AES算法可用于对数据进行加密码与解密,使用的时候需要注意两点:1)被加密的串越长,加密后的字符串越长,注意数据库字段的设计;2)Linux与Windows环境中可能会出现由于环境差异导致在Windows中测试成功,到Linux上后加密的串无法被正确解密。下列算法已在真实环境中进行实测,应用时也务必做好二次验证避免出现线上事故。
private static final String ALGORITHM_NAME = "AES";
//加密因子,可根据您的需要自定义
private static final String DEFAULT_ENCRYPT_RULE = "AES/CBC/PKCS5Padding";
private static final String RANDOM_KEY_ALGORITHM = "SHA1PRNG";
private static final String RANDOM_KEY_ALGORITHM_PROVIDER = "SUN";
public static String encrypt(String content) {
if (StringUtils.isEmpty(content)) {
return null;
}
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_NAME);
SecureRandom secureRandom = SecureRandom.getInstance(RANDOM_KEY_ALGORITHM, RANDOM_KEY_ALGORITHM_PROVIDER);
secureRandom.setSeed(DEFAULT_ENCRYPT_RULE.getBytes());
keyGenerator.init(128, secureRandom);
SecretKey originalKey = keyGenerator.generateKey();
SecretKey secretKey = new SecretKeySpec(originalKey.getEncoded(), ALGORITHM_NAME);
Cipher cipher = Cipher.getInstance(ALGORITHM_NAME);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
String result = new String(Base64.getEncoder().encodeToString(encrypted));
return result;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
* 解密
* @param encrypted 加密后的base64格式的密文
* @return 解密后的原文,出现异常时返回null
public static String decrypt(String encrypted) {
if (StringUtils.isEmpty(encrypted)) {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encrypted));
return new String(decrypted, "utf-8");
补充:下面看下Java使用AES算法的实例代码。
Java中使用AES(CBC,128位)算法加解密。一般加密后都是用一定编码格式进行传输,此处使用Base64算法进行编解码。实现及测试代码如下:
AESUtil.java
package gj.secure;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
public class AESUtil {
private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
private static final String KEY_ALGORITHM = "AES";
public static byte[] initKey() throws Exception {
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
kg.init(128);
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
}
public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
Key k = new SecretKeySpec(key, KEY_ALGORITHM);
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, k, paramSpec);
return cipher.doFinal(data);
public static byte[] decrypt(byte[] bytes, byte[] key, byte[] iv) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, k, paramSpec);
return cipher.doFinal(bytes);
public static String encodeToBase64String(String data, byte[] key, byte[] iv) throws Exception {
return Base64.getEncoder().encodeToString(encrypt(data.getBytes(), key, iv));
public static String decodeFromBase64String(String data, byte[] key, byte[] iv) throws Exception {
byte[] bytes = Base64.getDecoder().decode(data);
return new String(decrypt(bytes, key, iv));
}
测试代码:
AESUtilTest.java
package gj.secure;
public class AESUtilTest {
public static void main(String[] args) throws Exception {
byte[] key = AESUtil.initKey();
byte[] iv = {0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF,
0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF};
String content = "areful1997";
String cipher = AESUtil.encodeToBase64String(content, key, iv);
System.out.println(cipher);
String plain = AESUtil.decodeFromBase64String(cipher, key, iv);
System.out.println(plain);
}
}
到此这篇关于Java实现AES算法的文章就介绍到这了,更多相关Java实现AES算法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341