1+ using System ;
2+ using System . IO ;
3+ using System . Security . Cryptography ;
4+ using System . Text ;
5+
6+ namespace SimApi . Helpers ;
7+
8+ public static class SimApiAesUtil
9+ {
10+ // 密钥长度:256位 (32字节)
11+ private const int KeySize = 256 ;
12+
13+ // 块大小:128位 (16字节)
14+ private const int BlockSize = 128 ;
15+
16+ // 加密模式 - 重命名以避免与枚举类型冲突
17+ private const CipherMode AesCipherMode = CipherMode . CBC ;
18+
19+ // 填充模式 - 重命名以避免与枚举类型冲突
20+ private const PaddingMode AesPaddingMode = PaddingMode . PKCS7 ;
21+
22+ /// <summary>
23+ /// AES 加密
24+ /// </summary>
25+ /// <param name="plainText">明文</param>
26+ /// <param name="key">字符串密钥(将被处理为256位)</param>
27+ /// <returns>加密后的Base64字符串(包含IV)</returns>
28+ public static string Encrypt ( string plainText , string key )
29+ {
30+ if ( string . IsNullOrEmpty ( plainText ) )
31+ throw new ArgumentNullException ( nameof ( plainText ) ) ;
32+ if ( string . IsNullOrEmpty ( key ) )
33+ throw new ArgumentNullException ( nameof ( key ) ) ;
34+
35+ // 处理密钥为指定长度
36+ var keyBytes = ProcessKey ( key ) ;
37+ // 生成随机IV
38+ var iv = GenerateRandomIv ( ) ;
39+
40+ using var aes = CreateAesProvider ( keyBytes , iv ) ;
41+ using var encryptor = aes . CreateEncryptor ( aes . Key , aes . IV ) ;
42+ using var ms = new MemoryStream ( ) ;
43+ // 先写入IV,解密时需要用到
44+ ms . Write ( iv , 0 , iv . Length ) ;
45+
46+ using ( var cs = new CryptoStream ( ms , encryptor , CryptoStreamMode . Write ) )
47+ using ( var sw = new StreamWriter ( cs ) )
48+ {
49+ sw . Write ( plainText ) ;
50+ }
51+
52+ return Convert . ToBase64String ( ms . ToArray ( ) ) ;
53+ }
54+
55+ /// <summary>
56+ /// AES 解密
57+ /// </summary>
58+ /// <param name="cipherText">加密后的Base64字符串</param>
59+ /// <param name="key">字符串密钥(与加密时相同)</param>
60+ /// <returns>解密后的明文</returns>
61+ public static string Decrypt ( string cipherText , string key )
62+ {
63+ if ( string . IsNullOrEmpty ( cipherText ) )
64+ throw new ArgumentNullException ( nameof ( cipherText ) ) ;
65+ if ( string . IsNullOrEmpty ( key ) )
66+ throw new ArgumentNullException ( nameof ( key ) ) ;
67+
68+ var cipherBytes = Convert . FromBase64String ( cipherText ) ;
69+
70+ // 从加密数据中提取IV
71+ var iv = new byte [ BlockSize / 8 ] ;
72+ Array . Copy ( cipherBytes , 0 , iv , 0 , iv . Length ) ;
73+
74+ // 处理密钥为指定长度
75+ var keyBytes = ProcessKey ( key ) ;
76+
77+ using var aes = CreateAesProvider ( keyBytes , iv ) ;
78+ using var decryptor = aes . CreateDecryptor ( aes . Key , aes . IV ) ;
79+ using var ms = new MemoryStream ( cipherBytes , iv . Length , cipherBytes . Length - iv . Length ) ;
80+ using var cs = new CryptoStream ( ms , decryptor , CryptoStreamMode . Read ) ;
81+ using var sr = new StreamReader ( cs ) ;
82+ return sr . ReadToEnd ( ) ;
83+ }
84+
85+ /// <summary>
86+ /// 处理密钥为指定长度(256位)
87+ /// </summary>
88+ private static byte [ ] ProcessKey ( string key )
89+ {
90+ // 使用SHA256哈希处理密钥,确保得到32字节(256位)的密钥
91+ return SHA256 . HashData ( Encoding . UTF8 . GetBytes ( key ) ) ;
92+ }
93+
94+ /// <summary>
95+ /// 生成随机初始化向量
96+ /// </summary>
97+ private static byte [ ] GenerateRandomIv ( )
98+ {
99+ using var aes = Aes . Create ( ) ;
100+ aes . BlockSize = BlockSize ;
101+ aes . GenerateIV ( ) ;
102+ return aes . IV ;
103+ }
104+
105+ /// <summary>
106+ /// 创建并配置AES加密服务提供器
107+ /// </summary>
108+ private static Aes CreateAesProvider ( byte [ ] key , byte [ ] iv )
109+ {
110+ var aes = Aes . Create ( ) ;
111+ aes . KeySize = KeySize ;
112+ aes . BlockSize = BlockSize ;
113+ aes . Mode = AesCipherMode ;
114+ aes . Padding = AesPaddingMode ;
115+ aes . Key = key ;
116+ aes . IV = iv ;
117+ return aes ;
118+ }
119+ }
0 commit comments