AES(Advanced Encryption Standard,高级加密标准)
maiaimei 2025/10/15
AES(Advanced Encryption Standard,高级加密标准)是一种对称加密算法,也是目前最广泛使用的加密标准之一。
# 主要特点
- 对称加密:加密和解密使用相同的密钥
- 分组密码:将数据分成128位的块进行处理
- 密钥长度:支持128位、192位和256位三种密钥长度
- 轮数:根据密钥长度不同,分别进行10轮、12轮或14轮加密
# 工作原理
AES使用替换-置换网络(SPN)结构,主要包含四个基本操作:
- 字节替换(SubBytes):使用S盒进行非线性替换
- 行移位(ShiftRows):对状态矩阵的行进行循环移位
- 列混合(MixColumns):对列进行线性变换
- 轮密钥加(AddRoundKey):与轮密钥进行异或运算
# 应用场景
- 数据传输加密:HTTPS、VPN、WiFi(WPA2/WPA3)
- 文件加密:磁盘加密、文档保护
- 数据库加密:敏感数据存储
- 金融系统:银行交易、支付系统
# 安全方面
AES被认为是安全的加密算法,至今没有发现有效的密码学攻击方法。AES-256被广泛用于需要高安全性的场合,包括政府和军事应用。
AES在2001年被美国国家标准与技术研究院(NIST)采纳为联邦信息处理标准,取代了之前的DES算法。
# 代码实现
Java实现
public class DataEncryption {
private static final String ALGORITHM = "AES";
private static final String SECRET_KEY = "MySecretKey12345"; // 16字符
public static String encrypt(String data) throws Exception {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decrypt(String encryptedData) throws Exception {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decoded = Base64.getDecoder().decode(encryptedData);
byte[] decrypted = cipher.doFinal(decoded);
return new String(decrypted);
}
}
// 使用加密数据构建URL
String encryptedUserId = DataEncryption.encrypt(userId);
String safeUrl = "https://example.com/page?data=" + URLEncoder.encode(encryptedUserId, "UTF-8");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
TypeScript实现
import { AES, enc } from 'crypto-js';
const SECRET_KEY = 'MySecretKey12345';
export const encrypt = (data: string): string =>
AES.encrypt(data, SECRET_KEY).toString();
export const decrypt = (encryptedData: string): string =>
AES.decrypt(encryptedData, SECRET_KEY).toString(enc.Utf8);
// 使用示例
const encryptedUserId = encrypt(userId);
const safeUrl = `https://example.com/page?data=${encodeURIComponent(encryptedUserId)}`;
const decryptedData = decrypt(encryptedUserId);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14