Skip to content

Commit 6630954

Browse files
committed
Update CreateRsaKey method, now support rsa size,default size is 2048. Add RSAFromString method
1 parent f0be86b commit 6630954

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

NETCore.Encrypt.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.3
4+
VisualStudioVersion = 15.0.26730.8
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2520DA96-86CD-4BD6-8807-95F27381BE1C}"
77
EndProject

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public static string RSAEncrypt(string publicKey, string srcString)
258258
Check.Argument.IsNotEmpty(publicKey, nameof(publicKey));
259259
Check.Argument.IsNotEmpty(srcString, nameof(srcString));
260260

261-
using (RSA rsa = RSA.Create(2048))
261+
using (RSA rsa = RSA.Create())
262262
{
263263
rsa.FromJsonString(publicKey);
264264
byte[] encryptBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(srcString), RSAEncryptionPadding.OaepSHA512);
@@ -276,7 +276,7 @@ public static string RSADecrypt(string privateKey, string srcString)
276276
Check.Argument.IsNotEmpty(privateKey, nameof(privateKey));
277277
Check.Argument.IsNotEmpty(srcString, nameof(srcString));
278278

279-
using (RSA rsa = RSA.Create(2048))
279+
using (RSA rsa = RSA.Create())
280280
{
281281
rsa.FromJsonString(privateKey);
282282
byte[] srcBytes = srcString.ToBytes();
@@ -286,11 +286,11 @@ public static string RSADecrypt(string privateKey, string srcString)
286286
}
287287

288288
/// <summary>
289-
/// RSA Instance
289+
/// RSA from json string
290290
/// </summary>
291-
/// <param name="rsaKey"></param>
291+
/// <param name="rsaKey">rsa json string</param>
292292
/// <returns></returns>
293-
public static RSA RSAInstance(string rsaKey)
293+
public static RSA RSAFromString(string rsaKey)
294294
{
295295
Check.Argument.IsNotEmpty(rsaKey, nameof(rsaKey));
296296
RSA rsa = RSA.Create();
@@ -302,12 +302,12 @@ public static RSA RSAInstance(string rsaKey)
302302
/// <summary>
303303
/// Create an RSA key
304304
/// </summary>
305+
/// <param name="keySizeInBits">the default size is 2048</param>
305306
/// <returns></returns>
306-
public static RSAKey CreateRsaKey()
307+
public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
307308
{
308-
using (RSA rsa = RSA.Create(2048))
309+
using (RSA rsa = RSA.Create((int) rsaSize))
309310
{
310-
311311
string publicKey = rsa.ToJsonString(false);
312312
string privateKey = rsa.ToJsonString(true);
313313

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal static string ToJsonString(this RSA rsa, bool includePrivateParameters)
7474
/// </summary>
7575
/// <param name="rsa">RSA实例<see cref="RSA"/></param>
7676
/// <param name="jsonString">RSA的Key序列化XML字符串</param>
77-
public static void FromXmlString(this RSA rsa, string xmlString)
77+
public static void FromLvccXmlString(this RSA rsa, string xmlString)
7878
{
7979
RSAParameters parameters = new RSAParameters();
8080

@@ -112,7 +112,7 @@ public static void FromXmlString(this RSA rsa, string xmlString)
112112
/// <param name="rsa">RSA实例<see cref="RSA"/></param>
113113
/// <param name="includePrivateParameters">是否包含私钥</param>
114114
/// <returns></returns>
115-
public static string ToXmlString(this RSA rsa, bool includePrivateParameters)
115+
public static string ToLvccXmlString(this RSA rsa, bool includePrivateParameters)
116116
{
117117
RSAParameters parameters = rsa.ExportParameters(includePrivateParameters);
118118

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NETCore.Encrypt
6+
{
7+
public enum RsaSize
8+
{
9+
R1024=1024,
10+
R2048=2048,
11+
R3072=3072,
12+
R4096=4096
13+
}
14+
}

src/NETCore.Encrypt/NETCore.Encrypt.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
<Company>Lvcc</Company>
77
<Product>Lvcc</Product>
88
<Authors>Lvcc</Authors>
9-
<Description>NETCore encrypt and decrpty tool,Include AES,RSA,MD5,SAH1,SAH256,SHA384,SHA512</Description>
9+
<Description>NETCore encrypt and decrpty tool,Include AES,RSA,MD5,SAH1,SAH256,SHA384,SHA512</Description>
1010
<Copyright>Copyright 2017 (c) Lvcc. All rights reserved</Copyright>
1111
<PackageLicenseUrl>https://github.com/myloveCc/NETCore.Encrypt/blob/master/License</PackageLicenseUrl>
1212
<PackageProjectUrl>https://github.com/myloveCc/NETCore.Encrypt</PackageProjectUrl>
1313
<RepositoryUrl>https://github.com/myloveCc/NETCore.Encrypt</RepositoryUrl>
1414
<RepositoryType>Github</RepositoryType>
15-
<AssemblyVersion>1.0.1.0</AssemblyVersion>
16-
<FileVersion>1.0.1.0</FileVersion>
17-
<Version>1.0.1</Version>
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>
1819
</PropertyGroup>
1920

2021
<ItemGroup>

test/NETCore.Encrypt.Tests/RSA_Tests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,17 @@ public void Rsa_Decrypt_EmptyData_Test()
9292
Assert.Throws<ArgumentException>(() => EncryptProvider.RSAEncrypt(rsaKey.PublicKey, srcString));
9393
}
9494

95-
[Fact(DisplayName = "Rsa instance test")]
96-
public void Rsa_Instance_Test()
95+
[Fact(DisplayName = "Rsa from json string test")]
96+
public void Rsa_From_JsonString_Test()
9797
{
9898
var rsaKey = EncryptProvider.CreateRsaKey();
9999

100100
var publicKey = rsaKey.PublicKey;
101101
var privateKey = rsaKey.PrivateKey;
102102

103-
var rsa = EncryptProvider.RSAInstance(publicKey);
103+
var rsa = EncryptProvider.RSAFromString(publicKey);
104104

105105
Assert.NotNull(rsa);
106-
Assert.Equal(2048, rsa.KeySize);
107106

108107
}
109108
}

0 commit comments

Comments
 (0)