AES加密算法是对称密钥加密中最流行的算法之一
这是我转自CSDN博客的详细解析:
一般的加密通常都是块加密,如果要加密超过块大小的数据,就需要涉及填充和链加密模式,文中提到的ECB和CBC等就是指链加密模式。这篇文档比较形象地介绍了AES加密算法中的一些模式转载过来。注意,还缺一种CTR的模式。
同时在文章的最后,贴出几对利用ECB and CBC模式得标准算法得到的码流串。
对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
**一. AES对称加密:**
![](https://box.kancloud.cn/2016-02-22_56caa8338cdcf.jpg)
AES加密
![](https://box.kancloud.cn/2016-02-22_56caa833a969f.jpg)
分组
**二.分组密码的填充**
分组密码的填充
e.g.:
![](https://box.kancloud.cn/2016-02-22_56caa833b9365.jpg)
PKCS#5填充方式
**三.流密码:**
![](https://box.kancloud.cn/2016-02-22_56caa833cd820.jpg)
**四.分组密码加密中的四种模式:**
**3.1 ECB模式**
![](https://box.kancloud.cn/2016-02-22_56caa833df046.jpg)
**优点:**
1.简单;
2.有利于并行计算;
3.误差不会被传送;
**缺点:**
1.不能隐藏明文的模式;
2.可能对明文进行主动攻击;
![](https://box.kancloud.cn/2016-02-22_56caa83407f03.jpg)
**3.2 CBC模式:**
![](https://box.kancloud.cn/2016-02-22_56caa834545e0.jpg)
**优点:**
1.不容易主动攻击,安全性好于ECB,适合传输长度长的报文,是SSL、IPSec的标准。
**缺点:**
1.不利于并行计算;
2.误差传递;
3.需要初始化向量IV
**3.3 CFB模式:**
![](https://box.kancloud.cn/2016-02-22_56caa8346ca4a.jpg)
**优点:**
1.隐藏了明文模式;
2.分组密码转化为流模式;
3.可以及时加密传送小于分组的数据;
**缺点:**
1.不利于并行计算;
2.误差传送:一个明文单元损坏影响多个单元;
3.唯一的IV;
**3.4 OFB模式:**
![](https://box.kancloud.cn/2016-02-22_56caa8348bce7.jpg)
**优点:**
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));
}
}
~~~