Skip to content

Commit 776270b

Browse files
committed
Merge branch 'dev'
2 parents 84c1d63 + 88226b9 commit 776270b

File tree

5 files changed

+235
-26
lines changed

5 files changed

+235
-26
lines changed

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
# NETCore.Encrypt
1+
# NETCore.Encrypt [中文文档](http://www.cnblogs.com/piscesLoveCc/p/7423205.html)
2+
[![NuGet](https://img.shields.io/nuget/v/NETCore.Encrypt.svg)](https://nuget.org/packages/NETCore.Encrypt)
3+
[![NETCore CLR](https://img.shields.io/badge/.NETCore%20Clr-2.0-brightgreen.svg)](https://www.microsoft.com/net/core)
4+
25
NETCore encrypt and decrpty tool,Include AES,RSA,MD5,SAH1,SAH256,SHA384,SHA512 and more
36

47
To install NETCore.Encrypt, run the following command in the [Package Manager Console](https://docs.microsoft.com/zh-cn/nuget/tools/package-manager-console)
58
```
6-
Install-Package NETCore.Encrypt -Version 2.0.1
9+
Install-Package NETCore.Encrypt -Version 2.0.2
710
```
811

912
***
@@ -51,8 +54,32 @@ Install-Package NETCore.Encrypt -Version 2.0.1
5154
var encryptedStr = "xxxx";
5255
var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key, iv);
5356
```
57+
## DES
58+
59+
- #### Create DES Key
60+
61+
```csharp
62+
63+
//des key length is 24 bit
64+
var desKey = EncryptProvider.CreateDesKey();
65+
66+
```
67+
68+
- #### DES encrypt
69+
70+
```csharp
71+
var srcString = "des encrypt";
72+
var encrypted = EncryptProvider.DESEncrypt(srcString, key);
73+
```
5474

55-
## RSA
75+
- #### DES decrypt
76+
77+
```csharp
78+
var encryptedStr = "xxxx";
79+
var decrypted = EncryptProvider.DESDecrypt(encryptedStr, key);
80+
```
81+
82+
## RSA
5683

5784
- #### Enum RsaSize
5885

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 117 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/NETCore.Encrypt/Extensions/Internal/RsaKeyExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static void FromJsonString(this RSA rsa, string jsonString)
4141
}
4242
catch
4343
{
44-
throw new Exception("Invalid XML RSA key.");
44+
throw new Exception("Invalid Json RSA key.");
4545
}
4646
rsa.ImportParameters(parameters);
4747
}

src/NETCore.Encrypt/NETCore.Encrypt.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<PackageProjectUrl>https://github.com/myloveCc/NETCore.Encrypt</PackageProjectUrl>
1313
<RepositoryUrl>https://github.com/myloveCc/NETCore.Encrypt</RepositoryUrl>
1414
<RepositoryType>Github</RepositoryType>
15-
<AssemblyVersion>2.0.1.0</AssemblyVersion>
16-
<FileVersion>2.0.1.0</FileVersion>
17-
<Version>2.0.1</Version>
18-
<PackageReleaseNotes>Update CreateRsaKey, Add RSAFromString</PackageReleaseNotes>
15+
<AssemblyVersion>2.0.2.0</AssemblyVersion>
16+
<FileVersion>2.0.2.0</FileVersion>
17+
<Version>2.0.2</Version>
18+
<PackageReleaseNotes>Add des encrypt and decrypt.</PackageReleaseNotes>
1919
</PropertyGroup>
2020

2121
<ItemGroup>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Xunit;
5+
using NETCore.Encrypt;
6+
7+
namespace NETCore.Encrypt.Tests
8+
{
9+
public class DES_Test
10+
{
11+
private readonly string _Key;
12+
13+
public DES_Test()
14+
{
15+
_Key = EncryptProvider.CreateDesKey();
16+
}
17+
18+
[Fact(DisplayName = "DES ke test")]
19+
public void Cretea_DESKey_Test()
20+
{
21+
22+
var DESKey = EncryptProvider.CreateDesKey();
23+
24+
//Assert
25+
Assert.NotNull(DESKey);
26+
Assert.Equal(24, DESKey.Length);
27+
}
28+
29+
30+
[Fact(DisplayName = "DES encrypt with empty data test")]
31+
public void DES_Encryt_EmptyData_Fail_Test()
32+
{
33+
var srcString = string.Empty;
34+
//Assert
35+
Assert.Throws<ArgumentException>(() => EncryptProvider.DESDecrypt(srcString, _Key));
36+
}
37+
38+
39+
[Fact(DisplayName = "DES encrypt with error key test")]
40+
public void DES_Encrypt_ErrorKey_Fail_Test()
41+
{
42+
var key = "1hyhuo";
43+
var srcString = "test DES encrypt";
44+
45+
//Assert
46+
Assert.Throws<ArgumentOutOfRangeException>(() => EncryptProvider.DESEncrypt(srcString, key));
47+
}
48+
49+
[Fact(DisplayName = "DES decrypt success test")]
50+
public void DES_Decryt_Success_Test()
51+
{
52+
var srcString = "test DES encrypt";
53+
54+
//Ack
55+
var encrypted = EncryptProvider.DESEncrypt(srcString, _Key);
56+
var decrypted = EncryptProvider.DESDecrypt(encrypted, _Key);
57+
58+
//Assert
59+
Assert.NotEmpty(encrypted);
60+
Assert.NotEmpty(decrypted);
61+
Assert.Equal(srcString, decrypted);
62+
}
63+
64+
[Fact(DisplayName = "DES decrypt with empty data test")]
65+
public void DES_Decrypt_EmptyData_Fail_Test()
66+
{
67+
var srcString = string.Empty;
68+
69+
//Assert
70+
Assert.Throws<ArgumentException>(() => EncryptProvider.DESDecrypt(srcString, _Key));
71+
}
72+
73+
[Fact(DisplayName = "DES decrypt with error key test")]
74+
public void DES_Decrypt_ErrorKey_Fail_Test()
75+
{
76+
var key = "dfafa"; //must be 24 bit
77+
var srcString = "test DES encrypt";
78+
79+
//Assert
80+
Assert.Throws<ArgumentOutOfRangeException>(() => EncryptProvider.DESDecrypt(srcString, key));
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)