From 93cd7a35f74a6ccb6e70aee2fab89ec239d2f8e7 Mon Sep 17 00:00:00 2001 From: wengdezhi Date: Fri, 10 Apr 2020 17:29:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drsa,CreateRsaKey=E5=88=9B?= =?UTF-8?q?=E5=BB=BApublicKey=20=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/NETCore.Encrypt/EncryptProvider.cs | 16 ++++++++++++---- test/NETCore.Encrypt.Tests/RSA_Tests.cs | 6 ++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/NETCore.Encrypt/EncryptProvider.cs b/src/NETCore.Encrypt/EncryptProvider.cs index c8d1e48..1f3517b 100644 --- a/src/NETCore.Encrypt/EncryptProvider.cs +++ b/src/NETCore.Encrypt/EncryptProvider.cs @@ -819,21 +819,29 @@ public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048) /// Create an RSA key /// /// rsa + /// /// - public static RSAKey CreateRsaKey(RSA rsa) + public static RSAKey CreateRsaKey(RSA rsa, bool includePrivate = true) { Check.Argument.IsNotNull(rsa, nameof(rsa)); string publicKey = rsa.ToJsonString(false); - string privateKey = rsa.ToJsonString(true); - return new RSAKey() + + var rsaKey = new RSAKey() { PublicKey = publicKey, - PrivateKey = privateKey, + Exponent = rsa.ExportParameters(false).Exponent.ToHexString(), Modulus = rsa.ExportParameters(false).Modulus.ToHexString() }; + + if (includePrivate) + { + string privateKey = rsa.ToJsonString(true); + rsaKey.PrivateKey = privateKey; + } + return rsaKey; } /// diff --git a/test/NETCore.Encrypt.Tests/RSA_Tests.cs b/test/NETCore.Encrypt.Tests/RSA_Tests.cs index 347c360..614ccf6 100644 --- a/test/NETCore.Encrypt.Tests/RSA_Tests.cs +++ b/test/NETCore.Encrypt.Tests/RSA_Tests.cs @@ -309,6 +309,12 @@ public void Rsa_From_Pem_Test() Assert.NotNull(rsaFromPublickPem); Assert.NotNull(rsaFromPublickPem); Assert.Equal(rsaFromPublickPem.KeySize, rsaFromPrivatePem.KeySize); + var privateKey= EncryptProvider.CreateRsaKey(rsaFromPrivatePem); + var publicKey = EncryptProvider.CreateRsaKey(rsaFromPublickPem,false); + var raw = "123123124"; + var signStr = EncryptProvider.RSASign(raw, privateKey.PrivateKey); + var result = EncryptProvider.RSAVerify(raw, signStr, publicKey.PublicKey); + Assert.True(result); }