1.AES 密鑰+偏移進行加密解密 結合 base64對加密之後的位元組數組進行編碼解碼(代碼如下)
最近項目上需要用到資料加密存儲,查詢資料後發現目前AES加密安全度非常高,具體原理可自行百度。但經網上搜到的實作示例運作都有各種問題,耗時一上午,終于調試運作成功,閑話少說,直接上代碼。
參考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。
public static String encrypt(String cleartext) {
//------------------------------------------AES加密-------------------------------------
//加密方式: AES128(CBC/PKCS5Padding) + Base64, 私鑰:aabbccddeeffgghh
try {
IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
//兩個參數,第一個為私鑰位元組數組, 第二個為加密方式 AES或者DES
SecretKeySpec key = new SecretKeySpec(ASE_KEY.getBytes(), "AES");
//執行個體化加密類,參數為加密方式,要寫全
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //PKCS5Padding比PKCS7Padding效率高,PKCS7Padding可支援IOS加解密
//初始化,此方法可以采用三種方式,按加密算法要求來添加。(1)無第三個參數(2)第三個參數為SecureRandom random = new SecureRandom();中random對象,随機數。(AES不可采用這種方法)(3)采用此代碼中的IVParameterSpec
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
//------------------------------------------base64編碼-------------------------------------
//加密操作,傳回加密後的位元組數組,然後需要編碼。主要編解碼方式有Base64, HEX, UUE,7bit等等。此處看伺服器需要什麼編碼方式
//byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));
//return new BASE64Encoder().encode(encryptedData);
//上下等同,隻是導入包不同
//加密後的位元組數組
byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));
//對加密後的位元組數組進行base64編碼
byte[] base64Data = org.apache.commons.codec.binary.Base64.encodeBase64(encryptedData);
//将base64編碼後的位元組數組轉化為字元串并傳回
return new String(base64Data);
//------------------------------------------/base64編碼-------------------------------------
} catch (Exception e) {
e.printStackTrace();
return "";
}
//------------------------------------------/AES加密-------------------------------------
}
public static String decrypt(String encrypted) {
//---------------------------------------AES解密----------------------------------------
try {
//---------------------------------------base64解碼---------------------------------------
//byte[] byteMi = new BASE64Decoder().decodeBuffer(encrypted);
//上下等同,隻是導入包不同
//将字元串轉化為base64編碼的位元組數組
byte[] encryptedBase64Bytes = encrypted.getBytes();
//将base64編碼的位元組數組轉化為在加密之後的位元組數組
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解密----------------------------------------
}
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));
}
}
學習AES加密的一些文章:
1.JAVA實作AES加密