AES加密算法是對稱密鑰加密中最流行的算法之一
這是我轉自CSDN博客的詳細解析:
一般的加密通常都是塊加密,如果要加密超過塊大小的數據,就需要涉及填充和鏈加密模式,文中提到的ECB和CBC等就是指鏈加密模式。這篇文檔比較形象地介紹了AES加密算法中的一些模式轉載過來。注意,還缺一種CTR的模式。
同時在文章的最后,貼出幾對利用ECB? and CBC模式得標準算法得到的碼流串。
?對稱加密和分組加密中的四種模式(ECB、CBC、CFB、OFB)
**一. AES對稱加密:**

???????????????????????????????????????????????????? ?AES加密

???????????????????????? 分組
?
?
**二.分組密碼的填充**
?????????????????????????????????????????????????? 分組密碼的填充
?
e.g.:

???????????????????????????????????????????????????????? PKCS#5填充方式
?
?
?
**三.流密碼:**

**四.分組密碼加密中的四種模式:**
**3.1 ECB模式**

**優點:**
1.簡單;
2.有利于并行計算;
3.誤差不會被傳送;
**缺點:**
1.不能隱藏明文的模式;
2.可能對明文進行主動攻擊;

?
**3.2 CBC模式:**

**優點:**
1.不容易主動攻擊,安全性好于ECB,適合傳輸長度長的報文,是SSL、IPSec的標準。
**缺點:**
1.不利于并行計算;
2.誤差傳遞;
3.需要初始化向量IV
**3.3 CFB模式:**

**優點:**
1.隱藏了明文模式;
2.分組密碼轉化為流模式;
3.可以及時加密傳送小于分組的數據;
**缺點:**
1.不利于并行計算;
2.誤差傳送:一個明文單元損壞影響多個單元;
3.唯一的IV;
?
**3.4 OFB模式:**

**優點:**
1.隱藏了明文模式;
2.分組密碼轉化為流模式;
3.可以及時加密傳送小于分組的數據;
**缺點:**
1.不利于并行計算;
2.對明文的主動攻擊是可能的;
3.誤差傳送:一個明文單元損壞影響多個單元;
幾個碼流串,經過了某款芯片的硬件加解密單元的測試
?
ECB
?????? 1
Key 0x2b7e151628aed2a6abf7158809cf4f3c
Before encrypt 0x6bc1bee22e409f96e93d7e117393172a
After encrypt 0x3ad77bb40d7a3660a89ecaf32466ef97
?????? 2
Key 0x6bc1bee22e409f96e93d7e117393172a
Before encrypt 0xEA24274E EA6C2A7D F78E3345 467F171D
After encrypt 0x6bc1bee22e409f96e93d7e117393172a
?
CBC
Key 0x2b7e151628aed2a6abf7158809cf4f3c
IV?? 0x000102030405060708090a0b0c0d0e0f
Before encrypt 0x6bc1bee22e409f96e93d7e117393172a
After encrypt 0x7649abac8119b246cee98e9b12e9197d
然后,這個是網上找到的算法代碼例子:
~~~
package com.login.aes;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
/**
?* Created with IntelliJ IDEA
?* To change this template use File | Settings | File Templates.
?*/
public class AESEncryptor {
??? /**
???? * AES加密
??? */
??? public static String encrypt(String seed, String cleartext) throws Exception {
??????? byte[] rawKey = getRawKey(seed.getBytes());
??????? byte[] result = encrypt(rawKey, cleartext.getBytes());
??????? return toHex(result);
??? }
?? /**
???? * AES解密
??? */
??? public static String decrypt(String seed, String encrypted) throws Exception {
??????? byte[] rawKey = getRawKey(seed.getBytes());
??????? byte[] enc = toByte(encrypted);
??????? byte[] result = decrypt(rawKey, enc);
??????? return new String(result);
??? }
?? private static byte[] getRawKey(byte[] seed) throws Exception {
??????? KeyGenerator kgen = KeyGenerator.getInstance("AES");
??????? SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
??????? sr.setSeed(seed);
??????? kgen.init(128, sr); // 192 and 256 bits may not be available
??????? SecretKey skey = kgen.generateKey();
??????? byte[] raw = skey.getEncoded();
??????? return raw;
??? }
?? private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
??????? SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
??????? Cipher cipher = Cipher.getInstance("AES");
??????? cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
??????? byte[] encrypted = cipher.doFinal(clear);
??????? return encrypted;
??? }
?? private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
??????? SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
??????? Cipher cipher = Cipher.getInstance("AES");
??????? cipher.init(Cipher.DECRYPT_MODE, skeySpec);
??????? byte[] decrypted = cipher.doFinal(encrypted);
??????? return decrypted;
??? }
?? public static String toHex(String txt) {
??????? return toHex(txt.getBytes());
??? }
??? public static String fromHex(String hex) {
??????? return new String(toByte(hex));
??? }
?? public static byte[] toByte(String hexString) {
??????? int len = hexString.length()/2;
??????? byte[] result = new byte[len];
??????? for (int i = 0; i < len; i++)
??????????? result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
??????? return result;
??? }
?? public static String toHex(byte[] buf) {
??????? if (buf == null)
??????????? return "";
??????? StringBuffer result = new StringBuffer(2*buf.length);
??????? for (int i = 0; i < buf.length; i++) {
??????????? appendHex(result, buf[i]);
??????? }
??????? return result.toString();
??? }
??? private final static String HEX = "0123456789ABCDEF";
??? private static void appendHex(StringBuffer sb, byte b) {
??????? sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
??? }
}
~~~