diff --git a/src/Essensoft.AspNetCore.Payment.Alipay/Utility/AlipaySignature.cs b/src/Essensoft.AspNetCore.Payment.Alipay/Utility/AlipaySignature.cs index 536d6d195..b83fb6a1f 100644 --- a/src/Essensoft.AspNetCore.Payment.Alipay/Utility/AlipaySignature.cs +++ b/src/Essensoft.AspNetCore.Payment.Alipay/Utility/AlipaySignature.cs @@ -2,7 +2,6 @@ using System.Security.Cryptography; using System.Text; using Essensoft.AspNetCore.Payment.Security; -using Org.BouncyCastle.X509; namespace Essensoft.AspNetCore.Payment.Alipay.Utility { @@ -32,25 +31,27 @@ public static string GetSignContent(IDictionary dictionary) public static string RSASignContent(string data, string privateKey, string signType) { - var key = RSAUtilities.GetRSAParametersFormRsaPrivateKey(privateKey); switch (signType) { + case "RSA1": + return SHA1WithRSA.Sign(data, privateKey); case "RSA2": - return SHA256WithRSA.Sign(data, key); + return SHA256WithRSA.Sign(data, privateKey); default: - return SHA1WithRSA.Sign(data, key); + return SHA1WithRSA.Sign(data, privateKey); } } public static bool RSACheckContent(string data, string sign, string publicKey, string signType) { - var key = RSAUtilities.GetRSAParametersFormPublicKey(publicKey); switch (signType) { + case "RSA1": + return SHA1WithRSA.Verify(data, sign, publicKey); case "RSA2": - return SHA256WithRSA.Verify(data, sign, key); + return SHA256WithRSA.Verify(data, sign, publicKey); default: - return SHA1WithRSA.Verify(data, sign, key); + return SHA1WithRSA.Verify(data, sign, publicKey); } } diff --git a/src/Essensoft.AspNetCore.Payment.Security/RSAUtilities.cs b/src/Essensoft.AspNetCore.Payment.Security/RSAUtilities.cs index c7f72f950..1254abcec 100644 --- a/src/Essensoft.AspNetCore.Payment.Security/RSAUtilities.cs +++ b/src/Essensoft.AspNetCore.Payment.Security/RSAUtilities.cs @@ -1,64 +1,13 @@ using System; -using System.Security.Cryptography; using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; -using Org.BouncyCastle.Security; namespace Essensoft.AspNetCore.Payment.Security { public static class RSAUtilities { - /// - /// -----BEGIN RSA PRIVATE KEY----- - /// ... - /// -----END RSA PRIVATE KEY----- - /// - /// - public static RSAParameters GetRSAParametersFormRsaPrivateKey(string privateKey) - { - if (string.IsNullOrEmpty(privateKey)) - { - throw new ArgumentNullException(nameof(privateKey)); - } - - var key = RsaPrivateKeyStructure.GetInstance(Convert.FromBase64String(privateKey)); - return new RSAParameters - { - D = key.PrivateExponent.ToByteArrayUnsigned(), - DP = key.Exponent1.ToByteArrayUnsigned(), - DQ = key.Exponent2.ToByteArrayUnsigned(), - Exponent = key.PublicExponent.ToByteArrayUnsigned(), - InverseQ = key.Coefficient.ToByteArrayUnsigned(), - Modulus = key.Modulus.ToByteArrayUnsigned(), - P = key.Prime1.ToByteArrayUnsigned(), - Q = key.Prime2.ToByteArrayUnsigned(), - }; - } - - /// - /// -----BEGIN PUBLIC KEY----- - /// ... - /// -----END PUBLIC KEY----- - /// - /// - public static RSAParameters GetRSAParametersFormPublicKey(string publicKey) - { - if (string.IsNullOrEmpty(publicKey)) - { - throw new ArgumentNullException(nameof(publicKey)); - } - - var key = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); - return new RSAParameters - { - Modulus = key.Modulus.ToByteArrayUnsigned(), - Exponent = key.Exponent.ToByteArrayUnsigned() - }; - } - /// /// -----BEGIN RSA PUBLIC KEY----- /// ... diff --git a/src/Essensoft.AspNetCore.Payment.Security/SHA1WithRSA.cs b/src/Essensoft.AspNetCore.Payment.Security/SHA1WithRSA.cs index a36a0ce72..4713a15ea 100644 --- a/src/Essensoft.AspNetCore.Payment.Security/SHA1WithRSA.cs +++ b/src/Essensoft.AspNetCore.Payment.Security/SHA1WithRSA.cs @@ -6,21 +6,26 @@ namespace Essensoft.AspNetCore.Payment.Security { public static class SHA1WithRSA { - public static string Sign(string data, RSAParameters privateKey) + public static string Sign(string data, string privateKey) { if (string.IsNullOrEmpty(data)) { throw new ArgumentNullException(nameof(data)); } + if (string.IsNullOrEmpty(privateKey)) + { + throw new ArgumentNullException(nameof(privateKey)); + } + using (var rsa = RSA.Create()) { - rsa.ImportParameters(privateKey); + rsa.ImportRSAPrivateKey(Convert.FromBase64String(privateKey), out var _); return Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(data), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1)); } } - public static bool Verify(string data, string sign, RSAParameters publicKey) + public static bool Verify(string data, string sign, string publicKey) { if (string.IsNullOrEmpty(data)) { @@ -32,9 +37,14 @@ public static bool Verify(string data, string sign, RSAParameters publicKey) throw new ArgumentNullException(nameof(sign)); } + if (string.IsNullOrEmpty(publicKey)) + { + throw new ArgumentNullException(nameof(publicKey)); + } + using (var rsa = RSA.Create()) { - rsa.ImportParameters(publicKey); + rsa.ImportSubjectPublicKeyInfo(Convert.FromBase64String(publicKey), out var _); return rsa.VerifyData(Encoding.UTF8.GetBytes(data), Convert.FromBase64String(sign), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1); } } diff --git a/src/Essensoft.AspNetCore.Payment.Security/SHA256WithRSA.cs b/src/Essensoft.AspNetCore.Payment.Security/SHA256WithRSA.cs index d14011f10..e7dcc67bd 100644 --- a/src/Essensoft.AspNetCore.Payment.Security/SHA256WithRSA.cs +++ b/src/Essensoft.AspNetCore.Payment.Security/SHA256WithRSA.cs @@ -6,21 +6,26 @@ namespace Essensoft.AspNetCore.Payment.Security { public static class SHA256WithRSA { - public static string Sign(string data, RSAParameters privateKey) + public static string Sign(string data, string privateKey) { if (string.IsNullOrEmpty(data)) { throw new ArgumentNullException(nameof(data)); } + if (string.IsNullOrEmpty(privateKey)) + { + throw new ArgumentNullException(nameof(privateKey)); + } + using (var rsa = RSA.Create()) { - rsa.ImportParameters(privateKey); + rsa.ImportRSAPrivateKey(Convert.FromBase64String(privateKey), out var _); return Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(data), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); } } - public static bool Verify(string data, string sign, RSAParameters publicKey) + public static bool Verify(string data, string sign, string publicKey) { if (string.IsNullOrEmpty(data)) { @@ -32,9 +37,14 @@ public static bool Verify(string data, string sign, RSAParameters publicKey) throw new ArgumentNullException(nameof(sign)); } + if (string.IsNullOrEmpty(publicKey)) + { + throw new ArgumentNullException(nameof(publicKey)); + } + using (var rsa = RSA.Create()) { - rsa.ImportParameters(publicKey); + rsa.ImportSubjectPublicKeyInfo(Convert.FromBase64String(publicKey), out var _); return rsa.VerifyData(Encoding.UTF8.GetBytes(data), Convert.FromBase64String(sign), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); } }