1.AES 密鑰+偏移進(jìn)行加密解密 結(jié)合 base64對加密之后的字節(jié)數(shù)組進(jìn)行編碼解碼(代碼如下)
最近項目上需要用到數(shù)據(jù)加密存儲窄潭,查詢資料后發(fā)現(xiàn)目前AES加密安全度非常高,具體原理可自行百度冠王。但經(jīng)網(wǎng)上搜到的實(shí)現(xiàn)示例運(yùn)行都有各種問題米丘,耗時一上午剑令,終于調(diào)試運(yùn)行成功,閑話少說拄查,直接上代碼吁津。
參考gfaming的博客
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AesEncodeUtil {
//初始向量(偏移)
public static final String VIPARA = "aabbccddeeffgghh"; //AES 為16bytes. DES 為8bytes
//編碼方式
public static final String bm = "UTF-8";
//私鑰 (密鑰)
private static final String ASE_KEY="aabbccddeeffgghh"; //AES固定格式為128/192/256 bits.即:16/24/32bytes。DES固定格式為128bits堕扶,即8bytes碍脏。
/**
* 加密
*
* @param cleartext 加密前的字符串
* @return 加密后的字符串
*/
public static String encrypt(String cleartext) {
//------------------------------------------AES加密-------------------------------------
//加密方式: AES128(CBC/PKCS5Padding) + Base64, 私鑰:aabbccddeeffgghh
try {
IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
//兩個參數(shù),第一個為私鑰字節(jié)數(shù)組稍算, 第二個為加密方式 AES或者DES
SecretKeySpec key = new SecretKeySpec(ASE_KEY.getBytes(), "AES");
//實(shí)例化加密類典尾,參數(shù)為加密方式,要寫全
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //PKCS5Padding比PKCS7Padding效率高糊探,PKCS7Padding可支持IOS加解密
//初始化钾埂,此方法可以采用三種方式河闰,按加密算法要求來添加。(1)無第三個參數(shù)(2)第三個參數(shù)為SecureRandom random = new SecureRandom();中random對象勃教,隨機(jī)數(shù)淤击。(AES不可采用這種方法)(3)采用此代碼中的IVParameterSpec
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
//------------------------------------------base64編碼-------------------------------------
//加密操作,返回加密后的字節(jié)數(shù)組,然后需要編碼故源。主要編解碼方式有Base64, HEX, UUE,7bit等等污抬。此處看服務(wù)器需要什么編碼方式
//byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));
//return new BASE64Encoder().encode(encryptedData);
//上下等同,只是導(dǎo)入包不同
//加密后的字節(jié)數(shù)組
byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));
//對加密后的字節(jié)數(shù)組進(jìn)行base64編碼
byte[] base64Data = org.apache.commons.codec.binary.Base64.encodeBase64(encryptedData);
//將base64編碼后的字節(jié)數(shù)組轉(zhuǎn)化為字符串并返回
return new String(base64Data);
//------------------------------------------/base64編碼-------------------------------------
} catch (Exception e) {
e.printStackTrace();
return "";
}
//------------------------------------------/AES加密-------------------------------------
}
/**
* 解密
*
* @param encrypted 解密前的字符串(也就是加密后的字符串)
* @return 解密后的字符串(也就是加密前的字符串)
*/
public static String decrypt(String encrypted) {
//---------------------------------------AES解密----------------------------------------
try {
//---------------------------------------base64解碼---------------------------------------
//byte[] byteMi = new BASE64Decoder().decodeBuffer(encrypted);
//上下等同绳军,只是導(dǎo)入包不同
//將字符串轉(zhuǎn)化為base64編碼的字節(jié)數(shù)組
byte[] encryptedBase64Bytes = encrypted.getBytes();
//將base64編碼的字節(jié)數(shù)組轉(zhuǎn)化為在加密之后的字節(jié)數(shù)組
byte[] byteMi = org.apache.commons.codec.binary.Base64.decodeBase64(encryptedBase64Bytes);
//---------------------------------------/base64解碼---------------------------------------
IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
SecretKeySpec key = new SecretKeySpec(
ASE_KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//與加密時不同MODE:Cipher.DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
byte[] decryptedData = cipher.doFinal(byteMi);
return new String(decryptedData, bm);
} catch (Exception e) {
e.printStackTrace();
return "";
}
//---------------------------------------/AES解密----------------------------------------
}
/**
* 測試
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String content = "98.5674";
// 加密
System.out.println("加密前:" + content);
String encryptResult = encrypt(content);
System.out.println("加密后:" + new String(encryptResult));
// 解密
String decryptResult = decrypt(encryptResult);
System.out.println("解密后:" + new String(decryptResult));
}
}
學(xué)習(xí)AES加密的一些文章:
1.JAVA實(shí)現(xiàn)AES加密