AES(Advanced Encryption Standard,高级加密标准)是一种对称加密算法,广泛用于数据加密和保护。它支持 128 位、192 位和 256 位密钥长度,具有安全性高、效率好的特点。以下是 AES 加密的基本原理和实现方式。
---
### 1. AES 加密的基本概念
- **对称加密**:加密和解密使用相同的密钥。
- **分组加密**:将数据分成固定长度的块(AES 的块大小为 128 位)。
- **密钥长度**:支持 128 位(16 字节)、192 位(24 字节)和 256 位(32 字节)。
- **加密模式**:
- ECB(电子密码本模式):简单但不安全,不推荐使用。
- CBC(密码分组链接模式):常用,需要初始化向量(IV)。
- GCM(Galois/Counter Mode):支持加密和认证,适合需要完整性和机密性的场景。
- **填充方式**:当数据长度不是块大小的整数倍时,需要进行填充(如 PKCS7)。
---
### 2. AES 加密的步骤
以 CBC 模式为例:
1. **生成密钥**:选择一个 128 位、192 位或 256 位的密钥。
2. **生成初始化向量(IV)**:IV 是一个随机值,用于增加加密的随机性。
3. **加密数据**:
- 将数据分块(每块 128 位)。
- 使用密钥和 IV 对每个块进行加密。
4. **解密数据**:
- 使用相同的密钥和 IV 对加密数据进行解密。
---
### 3. AES 加密的实现(以 Python 为例)
以下是使用 Python 的 `cryptography` 库实现 AES 加密和解密的示例:
#### 安装依赖
```bash
pip install cryptography
```
#### 示例代码
```python
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
import os
# 生成密钥和 IV
key = os.urandom(32) # 256 位密钥
iv = os.urandom(16) # 128 位 IV
# 加密函数
def aes_encrypt(plaintext, key, iv):
# 创建 AES 加密器
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
# 填充数据
padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(plaintext.encode()) + padder.finalize()
# 加密
ciphertext = encryptor.update(padded_data) + encryptor.finalize()
return ciphertext
# 解密函数
def aes_decrypt(ciphertext, key, iv):
# 创建 AES 解密器
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
# 解密
padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
# 去除填充
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
plaintext = unpadder.update(padded_plaintext) + unpadder.finalize()
return plaintext.decode()
# 测试
plaintext = "Hello, AES!"
ciphertext = aes_encrypt(plaintext, key, iv)
print("加密后的数据:", ciphertext.hex())
decrypted_text = aes_decrypt(ciphertext, key, iv)
print("解密后的数据:", decrypted_text)
```
---
### 4. AES 加密的实现(以 Java 为例)
以下是使用 Java 的 `javax.crypto` 包实现 AES 加密和解密的示例:
#### 示例代码
```java
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESExample {
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
public static String encrypt(String plaintext, String key, String iv) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext, String key, String iv) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
return new String(decryptedBytes);
}
public static void main(String[] args) throws Exception {
String key = "12345678901234567890123456789012"; // 32 字节密钥
String iv = "1234567890123456"; // 16 字节 IV
String plaintext = "Hello, AES!";
String ciphertext = encrypt(plaintext, key, iv);
System.out.println("加密后的数据: " + ciphertext);
String decryptedText = decrypt(ciphertext, key, iv);
System.out.println("解密后的数据: " + decryptedText);
}
}
```
---
### 5. 注意事项
1. **密钥管理**:密钥需要安全存储,避免泄露。
2. **IV 的唯一性**:每次加密时使用不同的 IV,以增强安全性。
3. **加密模式选择**:推荐使用 CBC 或 GCM 模式,避免使用 ECB 模式。
4. **填充方式**:确保加密和解密时使用相同的填充方式。
---
### 6. 总结
AES 是一种高效且安全的对称加密算法,适用于各种数据加密场景。通过选择合适的加密模式、密钥长度和填充方式,可以确保数据的安全性和完整性。以上示例展示了如何在 Python 和 Java 中实现 AES 加密和解密。