java加解密之AES使用

上一次的隨筆記錄了關於DES加解密算法的使用,這次的隨筆是關於AES的。

因爲各種算法名字,模式名字很多都是三個字母,太容易混淆了,所以單獨寫成一篇。

AES加密也是用得比較多的,在逆向分析中也經常遇到,加密的強度也是妥妥的,傳說,它是爲了解決DES的過時而徵集的算法~~根源就不說了。


-----------------開始分割線---------------------

同DES那篇博客一樣,原理不說,說了也不懂,直接先上一個標準的加解密使用方法:

public class AESCrptography {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String content="hello";
		String key="aaaaaaaa";
		String iv="abcdefghijklmnop";
		
		System.out.println("加密前:"+byteToHexString(content.getBytes()));
		byte[ ] encrypted=AES_CBC_Encrypt(content.getBytes(), key.getBytes(), iv.getBytes());
		System.out.println("加密後:"+byteToHexString(encrypted));
		byte[ ] decrypted=AES_CBC_Decrypt(encrypted, key.getBytes(), iv.getBytes());
		System.out.println("解密後:"+byteToHexString(decrypted));
	}
	
	public static byte[] AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv){
		
		try{
			KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
			keyGenerator.init(128, new SecureRandom(keyBytes));
			SecretKey key=keyGenerator.generateKey();
			Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
			byte[] result=cipher.doFinal(content);
			return result;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("exception:"+e.toString());
		} 
		return null;
	}
	
	public static byte[] AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv){
		
		try{
			KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
			keyGenerator.init(128, new SecureRandom(keyBytes));//key長可設爲128,192,256位,這裏只能設爲128
			SecretKey key=keyGenerator.generateKey();
			Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
			byte[] result=cipher.doFinal(content);
			return result;
		}catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("exception:"+e.toString());
		} 
		return null;
	}
		
	public static String byteToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer(bytes.length);
        String sTemp;
        for (int i = 0; i < bytes.length; i++) {
            sTemp = Integer.toHexString(0xFF & bytes[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
}
運行結果:


AES的一個標準使用方法到此爲止

-------------------結束分割線--------------------

怎麼可能善罷甘休,DES都研究了,AES也得研究研究~~

首先,加解密。

跟DES一樣,直接設置Cypher.ENCRYPT 或 Cypher.DECRYPT 即可。

其次,key的生成模式。

上面的代碼用了 KeyGenerator 生成key,並沒有用 SecretFactory,原因是 拋異常 SecretFactory 找不到 該算法,爲什麼?我一臉懵逼~


根據 Oracle的文檔 Standard Algorithm Name Documentation 裏明明寫着支持 “AES” 的啊。。。


好吧,那麼只能暫時用Generator生成key了,但這裏接着又發生了一個問題,KeyGenerator初始化時,爲什麼只能128位?不是說可以192, 256的嗎?

看看jdk先:


JDK文檔中標註着128,額,好吧,那就128吧~

除了用KeyGenerator生成key之外,也可以直接使用原始key:

SecretKey key2=new SecretKeySpec(keyBytes, "AES");
不過,需要注意的是,這種方法的key的原始長度必須是128位,不然拋異常~~

而Generator的不會,因爲它會根據設置的位數(128)和SecureRandom自動生成相應位數(128)的key。

最後,塊加密模式和填充。

這裏跟上一篇介紹DES的一樣,畢竟AES和DES都是對稱加密的塊加密類型。


最後的最後,還有一個值得注意的問題。

Cipher.getInstance("AES")    和    Cipher.getInstance("AES/CBC/PKCS5Padding")   一樣,即默認。

附上jdk說明:


好了,隨筆到此爲止~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章