@@ -12,20 +12,7 @@ namespace NETCore.Encrypt
1212{
1313 public class EncryptProvider
1414 {
15- #region AES
16-
17- /// <summary>
18- /// create ase key
19- /// </summary>
20- /// <returns></returns>
21- public static AESKey CreateAesKey ( )
22- {
23- return new AESKey ( )
24- {
25- Key = GetRandomStr ( 32 ) ,
26- IV = GetRandomStr ( 16 )
27- } ;
28- }
15+ #region Common
2916
3017 /// <summary>
3118 /// Generate a random key
@@ -51,6 +38,24 @@ private static string GetRandomStr(int length)
5138 return num . ToString ( ) ;
5239 }
5340
41+
42+ #endregion
43+
44+ #region AES
45+
46+ /// <summary>
47+ /// Create ase key
48+ /// </summary>
49+ /// <returns></returns>
50+ public static AESKey CreateAesKey ( )
51+ {
52+ return new AESKey ( )
53+ {
54+ Key = GetRandomStr ( 32 ) ,
55+ IV = GetRandomStr ( 16 )
56+ } ;
57+ }
58+
5459 /// <summary>
5560 /// AES encrypt
5661 /// </summary>
@@ -202,7 +207,7 @@ public static string AESEncrypt(string data, string key)
202207 /// <summary>
203208 /// AES decrypt( no IV)
204209 /// </summary>
205- /// <param name="data">encrypted data</param>
210+ /// <param name="data">Encrypted data</param>
206211 /// <param name="key">Key, requires 32 bits</param>
207212 /// <returns>Decrypted string</returns>
208213 public static string AESDecrypt ( string data , string key )
@@ -230,8 +235,8 @@ public static string AESDecrypt(string data, string key)
230235 {
231236 try
232237 {
233- byte [ ] tmp = new byte [ encryptedBytes . Length + 32 ] ;
234- int len = cryptoStream . Read ( tmp , 0 , encryptedBytes . Length + 32 ) ;
238+ byte [ ] tmp = new byte [ encryptedBytes . Length ] ;
239+ int len = cryptoStream . Read ( tmp , 0 , encryptedBytes . Length ) ;
235240 byte [ ] ret = new byte [ len ] ;
236241 Array . Copy ( tmp , 0 , ret , 0 , len ) ;
237242 return Encoding . UTF8 . GetString ( ret ) ;
@@ -246,6 +251,101 @@ public static string AESDecrypt(string data, string key)
246251 }
247252 #endregion
248253
254+ #region DES
255+
256+ /// <summary>
257+ /// Create des key
258+ /// </summary>
259+ /// <returns></returns>
260+ public static string CreateDesKey ( )
261+ {
262+ return GetRandomStr ( 24 ) ;
263+ }
264+
265+ /// <summary>
266+ /// DES encrypt
267+ /// </summary>
268+ /// <param name="data">Raw data</param>
269+ /// <param name="key">Key, requires 24 bits</param>
270+ /// <returns>Encrypted string</returns>
271+ public static string DESEncrypt ( string data , string key )
272+ {
273+ Check . Argument . IsNotEmpty ( data , nameof ( data ) ) ;
274+ Check . Argument . IsNotEmpty ( key , nameof ( key ) ) ;
275+ Check . Argument . IsNotOutOfRange ( key . Length , 24 , 24 , nameof ( key ) ) ;
276+
277+ using ( MemoryStream mStream = new MemoryStream ( ) )
278+ {
279+ using ( TripleDES des = TripleDES . Create ( ) )
280+ {
281+ byte [ ] plainBytes = Encoding . UTF8 . GetBytes ( data ) ;
282+ Byte [ ] bKey = new Byte [ 24 ] ;
283+ Array . Copy ( Encoding . UTF8 . GetBytes ( key . PadRight ( bKey . Length ) ) , bKey , bKey . Length ) ;
284+
285+ des . Mode = CipherMode . ECB ;
286+ des . Padding = PaddingMode . PKCS7 ;
287+ des . Key = bKey ;
288+ using ( CryptoStream cryptoStream = new CryptoStream ( mStream , des . CreateEncryptor ( ) , CryptoStreamMode . Write ) )
289+ {
290+ try
291+ {
292+ cryptoStream . Write ( plainBytes , 0 , plainBytes . Length ) ;
293+ cryptoStream . FlushFinalBlock ( ) ;
294+ return Convert . ToBase64String ( mStream . ToArray ( ) ) ;
295+ }
296+ catch ( Exception ex )
297+ {
298+ return null ;
299+ }
300+ }
301+ }
302+ }
303+ }
304+
305+ /// <summary>
306+ /// DES decrypt
307+ /// </summary>
308+ /// <param name="data">Encrypted data</param>
309+ /// <param name="key">Key, requires 24 bits</param>
310+ /// <returns>Decrypted string</returns>
311+ public static string DESDecrypt ( string data , string key )
312+ {
313+ Check . Argument . IsNotEmpty ( data , nameof ( data ) ) ;
314+ Check . Argument . IsNotEmpty ( key , nameof ( key ) ) ;
315+ Check . Argument . IsNotOutOfRange ( key . Length , 24 , 24 , nameof ( key ) ) ;
316+
317+ Byte [ ] encryptedBytes = Convert . FromBase64String ( data ) ;
318+ Byte [ ] bKey = new Byte [ 24 ] ;
319+ Array . Copy ( Encoding . UTF8 . GetBytes ( key . PadRight ( bKey . Length ) ) , bKey , bKey . Length ) ;
320+
321+ using ( MemoryStream mStream = new MemoryStream ( encryptedBytes ) )
322+ {
323+ using ( TripleDES des = TripleDES . Create ( ) )
324+ {
325+ des . Mode = CipherMode . ECB ;
326+ des . Padding = PaddingMode . PKCS7 ;
327+ des . Key = bKey ;
328+ using ( CryptoStream cryptoStream = new CryptoStream ( mStream , des . CreateDecryptor ( ) , CryptoStreamMode . Read ) )
329+ {
330+ try
331+ {
332+ byte [ ] tmp = new byte [ encryptedBytes . Length ] ;
333+ int len = cryptoStream . Read ( tmp , 0 , encryptedBytes . Length ) ;
334+ byte [ ] ret = new byte [ len ] ;
335+ Array . Copy ( tmp , 0 , ret , 0 , len ) ;
336+ return Encoding . UTF8 . GetString ( ret ) ;
337+ }
338+ catch
339+ {
340+ return null ;
341+ }
342+ }
343+ }
344+ }
345+ }
346+
347+ #endregion
348+
249349 #region RSA
250350 /// <summary>
251351 /// RSA encrypt
@@ -636,7 +736,6 @@ private static string CreateMachineKey(int length)
636736
637737 #region Base64
638738
639-
640739 #region Base64加密解密
641740 /// <summary>
642741 /// Base64 encrypt
0 commit comments