Java實現(xiàn)AES算法的實例代碼
使用AES算法可用于對數(shù)據(jù)進(jìn)行加密碼與解密,使用的時候需要注意兩點:1)被加密的串越長,加密后的字符串越長,注意數(shù)據(jù)庫字段的設(shè)計;2)Linux與Windows環(huán)境中可能會出現(xiàn)由于環(huán)境差異導(dǎo)致在Windows中測試成功,到Linux上后加密的串無法被正確解密。下列算法已在真實環(huán)境中進(jìn)行實測,應(yīng)用時也務(wù)必做好二次驗證避免出現(xiàn)線上事故。
private static final String ALGORITHM_NAME = "AES";
//加密因子,可根據(jù)您的需要自定義
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";
/**
* AES加密
* @param content 待加密的內(nèi)容,為空時為回空
* @return 加密后的base64格式的結(jié)果,出現(xiàn)異常時返回null
*/
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 解密后的原文,出現(xiàn)異常時返回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");補(bǔ)充:下面看下Java使用AES算法的實例代碼。
Java中使用AES(CBC,128位)算法加解密。一般加密后都是用一定編碼格式進(jìn)行傳輸,此處使用Base64算法進(jìn)行編解碼。實現(xiàn)及測試代碼如下:
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;
/**
* created by gj on 2019-12-24 17:17
**/
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);
}
}到此這篇關(guān)于Java實現(xiàn)AES算法的文章就介紹到這了,更多相關(guān)Java實現(xiàn)AES算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot RestTemplate提交表單數(shù)據(jù)的三種方法
本篇文章主要介紹了Spring Boot RestTemplate提交表單數(shù)據(jù)的三種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
java數(shù)據(jù)隨機(jī)分頁實現(xiàn)方案
本文主要介紹了java數(shù)據(jù)隨機(jī)分頁實現(xiàn)方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

