Skip to content

Commit f0be86b

Browse files
committed
Add rsa instance function
1 parent d95d5ae commit f0be86b

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static string GetRandomStr(int length)
4343
StringBuilder num = new StringBuilder();
4444

4545
Random rnd = new Random(DateTime.Now.Millisecond);
46-
for (int i = 0; i < length; i++)
46+
for (int i = 0;i < length;i++)
4747
{
4848
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
4949
}
@@ -258,10 +258,10 @@ 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())
261+
using (RSA rsa = RSA.Create(2048))
262262
{
263263
rsa.FromJsonString(publicKey);
264-
byte[] encryptBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(srcString), RSAEncryptionPadding.OaepSHA1);
264+
byte[] encryptBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(srcString), RSAEncryptionPadding.OaepSHA512);
265265
return encryptBytes.ToHexString();
266266
}
267267
}
@@ -276,22 +276,38 @@ 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())
279+
using (RSA rsa = RSA.Create(2048))
280280
{
281281
rsa.FromJsonString(privateKey);
282282
byte[] srcBytes = srcString.ToBytes();
283-
byte[] decryptBytes = rsa.Decrypt(srcBytes, RSAEncryptionPadding.OaepSHA1);
283+
byte[] decryptBytes = rsa.Decrypt(srcBytes, RSAEncryptionPadding.OaepSHA512);
284284
return Encoding.UTF8.GetString(decryptBytes);
285285
}
286286
}
287+
288+
/// <summary>
289+
/// RSA Instance
290+
/// </summary>
291+
/// <param name="rsaKey"></param>
292+
/// <returns></returns>
293+
public static RSA RSAInstance(string rsaKey)
294+
{
295+
Check.Argument.IsNotEmpty(rsaKey, nameof(rsaKey));
296+
RSA rsa = RSA.Create();
297+
298+
rsa.FromJsonString(rsaKey);
299+
return rsa;
300+
}
301+
287302
/// <summary>
288303
/// Create an RSA key
289304
/// </summary>
290305
/// <returns></returns>
291306
public static RSAKey CreateRsaKey()
292307
{
293-
using (RSA rsa = RSA.Create())
308+
using (RSA rsa = RSA.Create(2048))
294309
{
310+
295311
string publicKey = rsa.ToJsonString(false);
296312
string privateKey = rsa.ToJsonString(true);
297313

@@ -609,7 +625,7 @@ private static string CreateMachineKey(int length)
609625
rng.GetBytes(random);
610626

611627
StringBuilder machineKey = new StringBuilder(length);
612-
for (int i = 0; i < random.Length; i++)
628+
for (int i = 0;i < random.Length;i++)
613629
{
614630
machineKey.Append(string.Format("{0:X2}", random[i]));
615631
}

test/NETCore.Encrypt.Tests/RSA_Tests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,20 @@ public void Rsa_Decrypt_EmptyData_Test()
9191
//Assert
9292
Assert.Throws<ArgumentException>(() => EncryptProvider.RSAEncrypt(rsaKey.PublicKey, srcString));
9393
}
94+
95+
[Fact(DisplayName = "Rsa instance test")]
96+
public void Rsa_Instance_Test()
97+
{
98+
var rsaKey = EncryptProvider.CreateRsaKey();
99+
100+
var publicKey = rsaKey.PublicKey;
101+
var privateKey = rsaKey.PrivateKey;
102+
103+
var rsa = EncryptProvider.RSAInstance(publicKey);
104+
105+
Assert.NotNull(rsa);
106+
Assert.Equal(2048, rsa.KeySize);
107+
108+
}
94109
}
95110
}

0 commit comments

Comments
 (0)