1
+ using System . Security . Cryptography ;
2
+ using System . Text ;
3
+
4
+ namespace GameFrameX . Utility ;
5
+
6
+ /// <summary>
7
+ /// 加密解密相关的实用函数。
8
+ /// </summary>
9
+ public static partial class Encryption
10
+ {
11
+ public static class Aes
12
+ {
13
+ #region 加密
14
+
15
+ #region 加密字符串
16
+
17
+ /// <summary>
18
+ /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
19
+ /// </summary>
20
+ /// <param name="EncryptString">待加密密文</param>
21
+ /// <param name="EncryptKey">加密密钥</param>
22
+ public static string AESEncrypt ( string EncryptString , string EncryptKey )
23
+ {
24
+ return Convert . ToBase64String ( AESEncrypt ( Encoding . UTF8 . GetBytes ( EncryptString ) , EncryptKey ) ) ;
25
+ }
26
+
27
+ #endregion
28
+
29
+ #region 加密字节数组
30
+
31
+ /// <summary>
32
+ /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
33
+ /// </summary>
34
+ /// <param name="EncryptString">待加密密文</param>
35
+ /// <param name="EncryptKey">加密密钥</param>
36
+ public static byte [ ] AESEncrypt ( byte [ ] EncryptByte , string EncryptKey )
37
+ {
38
+ if ( EncryptByte . Length == 0 )
39
+ {
40
+ throw ( new Exception ( "明文不得为空" ) ) ;
41
+ }
42
+
43
+ if ( string . IsNullOrEmpty ( EncryptKey ) )
44
+ {
45
+ throw ( new Exception ( "密钥不得为空" ) ) ;
46
+ }
47
+
48
+ byte [ ] m_strEncrypt ;
49
+ byte [ ] m_btIV = new byte [ 16 ] { 224 , 131 , 122 , 101 , 37 , 254 , 33 , 17 , 19 , 28 , 212 , 130 , 45 , 65 , 43 , 32 } ;
50
+ byte [ ] m_salt = new byte [ 16 ] { 234 , 231 , 123 , 100 , 87 , 254 , 123 , 17 , 89 , 18 , 230 , 13 , 45 , 65 , 43 , 32 } ;
51
+ Rijndael m_AESProvider = Rijndael . Create ( ) ;
52
+ try
53
+ {
54
+ MemoryStream m_stream = new MemoryStream ( ) ;
55
+ PasswordDeriveBytes pdb = new PasswordDeriveBytes ( EncryptKey , m_salt ) ;
56
+ ICryptoTransform transform = m_AESProvider . CreateEncryptor ( pdb . GetBytes ( 32 ) , m_btIV ) ;
57
+ CryptoStream m_csstream = new CryptoStream ( m_stream , transform , CryptoStreamMode . Write ) ;
58
+ m_csstream . Write ( EncryptByte , 0 , EncryptByte . Length ) ;
59
+ m_csstream . FlushFinalBlock ( ) ;
60
+ m_strEncrypt = m_stream . ToArray ( ) ;
61
+ m_stream . Close ( ) ;
62
+ m_stream . Dispose ( ) ;
63
+ m_csstream . Close ( ) ;
64
+ m_csstream . Dispose ( ) ;
65
+ }
66
+ catch ( IOException ex )
67
+ {
68
+ throw ex ;
69
+ }
70
+ catch ( CryptographicException ex )
71
+ {
72
+ throw ex ;
73
+ }
74
+ catch ( ArgumentException ex )
75
+ {
76
+ throw ex ;
77
+ }
78
+ catch ( Exception ex )
79
+ {
80
+ throw ex ;
81
+ }
82
+ finally
83
+ {
84
+ m_AESProvider . Clear ( ) ;
85
+ }
86
+
87
+ return m_strEncrypt ;
88
+ }
89
+
90
+ #endregion
91
+
92
+ #endregion
93
+
94
+ #region 解密
95
+
96
+ #region 解密字符串
97
+
98
+ /// <summary>
99
+ /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
100
+ /// </summary>
101
+ /// <param name="DecryptString">待解密密文</param>
102
+ /// <param name="DecryptKey">解密密钥</param>
103
+ public static string AESDecrypt ( string DecryptString , string DecryptKey )
104
+ {
105
+ return Encoding . UTF8 . GetString ( ( AESDecrypt ( Convert . FromBase64String ( DecryptString ) , DecryptKey ) ) ) ;
106
+ }
107
+
108
+ #endregion
109
+
110
+ #region 解密字节数组
111
+
112
+ /// <summary>
113
+ /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
114
+ /// </summary>
115
+ /// <param name="DecryptString">待解密密文</param>
116
+ /// <param name="DecryptKey">解密密钥</param>
117
+ public static byte [ ] AESDecrypt ( byte [ ] DecryptByte , string DecryptKey )
118
+ {
119
+ if ( DecryptByte . Length == 0 )
120
+ {
121
+ throw ( new Exception ( "密文不得为空" ) ) ;
122
+ }
123
+
124
+ if ( string . IsNullOrEmpty ( DecryptKey ) )
125
+ {
126
+ throw ( new Exception ( "密钥不得为空" ) ) ;
127
+ }
128
+
129
+ byte [ ] m_strDecrypt ;
130
+ byte [ ] m_btIV = new byte [ 16 ] { 224 , 131 , 122 , 101 , 37 , 254 , 33 , 17 , 19 , 28 , 212 , 130 , 45 , 65 , 43 , 32 } ;
131
+ byte [ ] m_salt = new byte [ 16 ] { 234 , 231 , 123 , 100 , 87 , 254 , 123 , 17 , 89 , 18 , 230 , 13 , 45 , 65 , 43 , 32 } ;
132
+ Rijndael m_AESProvider = Rijndael . Create ( ) ;
133
+ try
134
+ {
135
+ MemoryStream m_stream = new MemoryStream ( ) ;
136
+ PasswordDeriveBytes pdb = new PasswordDeriveBytes ( DecryptKey , m_salt ) ;
137
+ ICryptoTransform transform = m_AESProvider . CreateDecryptor ( pdb . GetBytes ( 32 ) , m_btIV ) ;
138
+ CryptoStream m_csstream = new CryptoStream ( m_stream , transform , CryptoStreamMode . Write ) ;
139
+ m_csstream . Write ( DecryptByte , 0 , DecryptByte . Length ) ;
140
+ m_csstream . FlushFinalBlock ( ) ;
141
+ m_strDecrypt = m_stream . ToArray ( ) ;
142
+ m_stream . Close ( ) ;
143
+ m_stream . Dispose ( ) ;
144
+ m_csstream . Close ( ) ;
145
+ m_csstream . Dispose ( ) ;
146
+ }
147
+ catch ( IOException ex )
148
+ {
149
+ throw ex ;
150
+ }
151
+ catch ( CryptographicException ex )
152
+ {
153
+ throw ex ;
154
+ }
155
+ catch ( ArgumentException ex )
156
+ {
157
+ throw ex ;
158
+ }
159
+ catch ( Exception ex )
160
+ {
161
+ throw ex ;
162
+ }
163
+ finally
164
+ {
165
+ m_AESProvider . Clear ( ) ;
166
+ }
167
+
168
+ return m_strDecrypt ;
169
+ }
170
+
171
+ #endregion
172
+
173
+ #endregion
174
+ }
175
+ }
0 commit comments