Skip to content

Commit 543361c

Browse files
committed
add new feature for des encryp/decrypt
1 parent dd268ed commit 543361c

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,33 @@ public static string DESEncrypt(string data, string key)
331331
Check.Argument.IsNotEmpty(key, nameof(key));
332332
Check.Argument.IsNotOutOfRange(key.Length, 24, 24, nameof(key));
333333

334+
byte[] plainBytes = Encoding.UTF8.GetBytes(data);
335+
var encryptBytes = DESEncrypt(plainBytes, key);
336+
337+
if (encryptBytes == null)
338+
{
339+
return null;
340+
}
341+
return Convert.ToBase64String(encryptBytes);
342+
}
343+
344+
/// <summary>
345+
/// DES encrypt
346+
/// </summary>
347+
/// <param name="data">Raw data</param>
348+
/// <param name="key">Key, requires 24 bits</param>
349+
/// <returns>Encrypted byte array</returns>
350+
public static byte[] DESEncrypt(byte[] data, string key)
351+
{
352+
Check.Argument.IsNotEmpty(data, nameof(data));
353+
Check.Argument.IsNotEmpty(key, nameof(key));
354+
Check.Argument.IsNotOutOfRange(key.Length, 24, 24, nameof(key));
355+
334356
using (MemoryStream Memory = new MemoryStream())
335357
{
336358
using (TripleDES des = TripleDES.Create())
337359
{
338-
byte[] plainBytes = Encoding.UTF8.GetBytes(data);
360+
Byte[] plainBytes = data;
339361
Byte[] bKey = new Byte[24];
340362
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
341363

@@ -348,7 +370,7 @@ public static string DESEncrypt(string data, string key)
348370
{
349371
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
350372
cryptoStream.FlushFinalBlock();
351-
return Convert.ToBase64String(Memory.ToArray());
373+
return Memory.ToArray();
352374
}
353375
catch (Exception ex)
354376
{
@@ -372,6 +394,28 @@ public static string DESDecrypt(string data, string key)
372394
Check.Argument.IsNotOutOfRange(key.Length, 24, 24, nameof(key));
373395

374396
Byte[] encryptedBytes = Convert.FromBase64String(data);
397+
Byte[] bytes = DESDecrypt(encryptedBytes, key);
398+
399+
if (bytes == null)
400+
{
401+
return null;
402+
}
403+
return Encoding.UTF8.GetString(bytes);
404+
}
405+
406+
/// <summary>
407+
/// DES decrypt
408+
/// </summary>
409+
/// <param name="data">Encrypted data</param>
410+
/// <param name="key">Key, requires 24 bits</param>
411+
/// <returns>Decrypted byte array</returns>
412+
public static byte[] DESDecrypt(byte[] data, string key)
413+
{
414+
Check.Argument.IsNotEmpty(data, nameof(data));
415+
Check.Argument.IsNotEmpty(key, nameof(key));
416+
Check.Argument.IsNotOutOfRange(key.Length, 24, 24, nameof(key));
417+
418+
Byte[] encryptedBytes = data;
375419
Byte[] bKey = new Byte[24];
376420
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
377421

@@ -390,7 +434,7 @@ public static string DESDecrypt(string data, string key)
390434
int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length);
391435
byte[] ret = new byte[len];
392436
Array.Copy(tmp, 0, ret, 0, len);
393-
return Encoding.UTF8.GetString(ret);
437+
return ret;
394438
}
395439
catch
396440
{

0 commit comments

Comments
 (0)