Skip to content

Commit 281aee9

Browse files
committed
Add ras method test and fix error.
1 parent dcbb2ee commit 281aee9

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public static string RSADecrypt(string privateKey, string srcString, RSAEncrypti
409409
{
410410
rsa.FromJsonString(privateKey);
411411
byte[] srcBytes = srcString.ToBytes();
412-
byte[] decryptBytes = rsa.Decrypt(srcBytes, RSAEncryptionPadding.Pkcs1);
412+
byte[] decryptBytes = rsa.Decrypt(srcBytes, padding);
413413
return Encoding.UTF8.GetString(decryptBytes);
414414
}
415415
}

test/NETCore.Encrypt.Tests/RSA_Tests.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text;
44
using Xunit;
55
using NETCore.Encrypt;
6+
using System.Security.Cryptography;
67

78
namespace NETCore.Encrypt.Tests
89
{
@@ -27,7 +28,6 @@ public void Create_RSAKey_Test(RsaSize size)
2728
}
2829

2930
[Theory]
30-
3131
[InlineData(RsaSize.R2048)]
3232
[InlineData(RsaSize.R3072)]
3333
[InlineData(RsaSize.R4096)]
@@ -43,6 +43,23 @@ public void Rsa_Encrypt_Success_Test(RsaSize size)
4343
Assert.NotEmpty(encrypted);
4444
}
4545

46+
[Theory(DisplayName = "Rsa encrypt with custom RSAEncryptionPadding")]
47+
[InlineData(RsaSize.R2048)]
48+
[InlineData(RsaSize.R3072)]
49+
[InlineData(RsaSize.R4096)]
50+
public void Rsa_Encrypt_WithPadding_Test(RsaSize size)
51+
{
52+
var rsaKey = EncryptProvider.CreateRsaKey(size);
53+
var srcString = "rsa encrypt";
54+
55+
//Act
56+
var encrypted = EncryptProvider.RSAEncrypt(rsaKey.PublicKey, srcString, RSAEncryptionPadding.OaepSHA512);
57+
58+
//Assert
59+
Assert.NotEmpty(encrypted);
60+
}
61+
62+
4663
[Fact(DisplayName = "Rsa encrypt fail with emtpy key")]
4764
public void Rsa_Encrypt_EmptyKey_Test()
4865
{
@@ -82,6 +99,28 @@ public void Rsa_Decrypt_Success_Test(RsaSize size)
8299
Assert.Equal(srcString, decrypted);
83100
}
84101

102+
103+
[Theory(DisplayName = "Rsa decrypt with custom RSAEncryptionPadding")]
104+
[InlineData(RsaSize.R2048)]
105+
[InlineData(RsaSize.R3072)]
106+
[InlineData(RsaSize.R4096)]
107+
public void Rsa_Decrypt_WithPadding_Test(RsaSize size)
108+
{
109+
var rsaKey = EncryptProvider.CreateRsaKey(size);
110+
var srcString = "rsa decrypt";
111+
112+
//Act
113+
var padding = RSAEncryptionPadding.OaepSHA512;
114+
var encrypted = EncryptProvider.RSAEncrypt(rsaKey.PublicKey, srcString, padding);
115+
var decrypted = EncryptProvider.RSADecrypt(rsaKey.PrivateKey, encrypted, padding);
116+
117+
//Assert
118+
Assert.NotEmpty(encrypted);
119+
Assert.NotEmpty(decrypted);
120+
Assert.Equal(srcString, decrypted);
121+
}
122+
123+
85124
[Fact(DisplayName = "Rsa decrypt fail with emtpy key")]
86125
public void Rsa_Decrypt_EmptyKey_Test()
87126
{

0 commit comments

Comments
 (0)