序
本文主要小結一下java里頭的AES以及RSA加解密。
AES
使用AES加密時需要幾個參數:
- 密鑰長度(Key Size)
AES算法下,key的長度有三種:128、192和256 bits。由于歷史原因,JDK默認只支持不大于128 bits的密鑰,而128 bits的key已能夠滿足商用安全需求。
- 加密模式(Cipher Mode)
分組密碼算法只能加密固定長度的分組,但是我們需要加密的明文長度可能會超過分組密碼的分組長度,這時就需要對分組密碼算法進行迭代,以便將一段很長的明文全部加密。而迭代的方法就稱為分組密碼的模式。
AES屬于塊加密(Block Cipher),塊加密中有CBC、ECB、CTR、OFB、CFB等幾種工作模式。
- ECB過于簡單而不安全(
ECB模式由于每塊數據的加密是獨立的因此加密和解密都可以并行計算,ECB模式最大的缺點是相同的明文塊會被加密成相同的密文塊,這種方法在某些環境下不能提供嚴格的數據保密性
); - CFB可被施以重放攻擊;
- OFB 和 CTR 都可被主動攻擊者反轉密文,而引起解密后明文中的相應比特也發生變化;CTR比之OFB,多出能支持并發計算的特性,此外CTR是流式密碼;
- CBC雖不支持并行計算,但是卻是這些模式中最為安全的
本文使用CBC模式。
CBC模式對于每個待加密的密碼塊在加密前會先與前一個密碼塊的密文異或然后再用加密器加密。第一個明文塊與一個叫初始化向量的數據塊異或。CBC模式相比ECB有更高的保密性,但由于對每個數據塊的加密依賴與前一個數據塊的加密所以加密無法并行。與ECB一樣在加密前需要對數據進行填充,不是很適合對流數據進行加密。
- 填充方式(Padding)
由于塊加密只能對特定長度的數據塊進行加密,因此CBC、ECB模式需要在最后一數據塊加密前進行數據填充。
JDK則提供了PKCS5Padding。
- 初始向量(Initialization Vector)
使用除ECB以外的其他加密模式均需要傳入一個初始向量,其大小與Block Size相等(AES的Block Size為128 bits)
生成AES KEY
public static String genKeyAES() throws Exception {
KeyGenerator kenGen = KeyGenerator.getInstance(AES);
kenGen.init(KEY_SIZE);
SecretKey key = kenGen.generateKey();
String base64 = Base64.getEncoder().encodeToString(key.getEncoded());
return base64;
}
這里KEY_SIZE采用128
后面統一用base64將byte[]轉為字符串,方便展示、排查。
加解密
public static byte[] aesEncryptBytes(byte[] contentBytes, byte[] keyBytes) throws Exception {
return cipherOperation(contentBytes, keyBytes, Cipher.ENCRYPT_MODE);
}
public static byte[] aesDecryptBytes(byte[] contentBytes, byte[] keyBytes) throws Exception {
return cipherOperation(contentBytes, keyBytes, Cipher.DECRYPT_MODE);
}
private static byte[] cipherOperation(byte[] contentBytes, byte[] keyBytes, int mode) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(keyBytes,AES);
byte[] initParam = SIXTEEN_CHAR_INIT_VECTOR.getBytes(CHARSET);
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
cipher.init(mode, secretKey, ivParameterSpec);
return cipher.doFinal(contentBytes);
}
這里AES_CBC_PKCS5_PADDING為AES/CBC/PKCS5Padding,使用簡寫的AES默認就是這個值
RSA
生成密鑰對
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA);
keyPairGenerator.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
public static String getPublicKey(KeyPair keyPair){
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return Base64.getEncoder().encodeToString(bytes);
}
public static String getPrivateKey(KeyPair keyPair){
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return Base64.getEncoder().encodeToString(bytes);
}
這里KEY_SIZE用1024,RSA256,RSA512,RSA1024,RSA2048這四種,RSA后面的N代表位數(多少bit),位數越大,加密強度越大,需要破解需要的時間也就越長
目前主流密鑰長度至少都是1024bits以上,低于1024bit的密鑰已經不建議使用(安全問題)。那么上限在哪里?沒有上限,多大都可以使用。所以,主流的模值是1024位
加解密
public static byte[] publicEncrypt(byte[] content,PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
public static byte[] privateDecrypt(byte[] content,PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE,privateKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
這里RSA即"RSA",默認是RSA/ECB/PKCS1Padding
AES與RSA結合
- RSA 比 AES 更難破解,因為它不需要擔心密鑰在傳遞過程中有泄露,只存在暴力破解一種可能;
- AES的優勢是以分組為輪,加解密速度非常快,一般而言,AES 速度上數百倍于 RSA
在實際應用中,我們會混合應用AES和RSA:
- 1、生成一個一次性隨機密鑰,算法上采用 AES 的CBC模式 aes-128-cbc(加密分組為128比特)對文件進行加密
- 2、加密完成后,為了安全的傳遞這個一次性隨機密鑰,我們使用接收方的RSA公鑰 對其進行加密,隨加密后的文件一起發送
- 3、接收方使用RSA私鑰進行解密,得到AES密鑰原文,并用AES解密文件
這樣就充分利用了兩者的優勢.
public void testHyperCodec(){
KeyPair keyPair = RSAUtil.getKeyPair();
String pubKey = RSAUtil.getPublicKey(keyPair);
String priKey = RSAUtil.getPrivateKey(keyPair);
String password = "1234567890";
String aesRawKey = AESUtil.genKeyAES();
System.out.println("aes raw key:"+aesRawKey);
String rsaEntryptAesKey = RSAUtil.publicEncryptString(aesRawKey,pubKey);
System.out.println(rsaEntryptAesKey);
String aesEntryptContent = AESUtil.aesEncryptStringByBase64Key(password,aesRawKey);
System.out.println(aesEntryptContent);
//decode
String decodedAesKey = RSAUtil.privateDecryptString(rsaEntryptAesKey,priKey);
String decodedPwd = AESUtil.aesDecryptStringByBase64Key(aesEntryptContent,decodedAesKey);
System.out.println("aes key decoded:"+decodedAesKey);
System.out.println(decodedPwd);
}