Skip to content

Commit

Permalink
使用 RSA.ImportRSAPrivateKey、RSA.ImportSubjectPublicKeyInfo 导入公私钥
Browse files Browse the repository at this point in the history
  • Loading branch information
Roc committed Dec 17, 2019
1 parent 88e75c2 commit 911283f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -32,25 +31,27 @@ public static string GetSignContent(IDictionary<string, string> 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);
}
}

Expand Down
51 changes: 0 additions & 51 deletions src/Essensoft.AspNetCore.Payment.Security/RSAUtilities.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// -----BEGIN RSA PRIVATE KEY-----
/// ...
/// -----END RSA PRIVATE KEY-----
/// </summary>
/// <param name="privateKey"></param>
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(),
};
}

/// <summary>
/// -----BEGIN PUBLIC KEY-----
/// ...
/// -----END PUBLIC KEY-----
/// </summary>
/// <param name="publicKey"></param>
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()
};
}

/// <summary>
/// -----BEGIN RSA PUBLIC KEY-----
/// ...
Expand Down
18 changes: 14 additions & 4 deletions src/Essensoft.AspNetCore.Payment.Security/SHA1WithRSA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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);
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/Essensoft.AspNetCore.Payment.Security/SHA256WithRSA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 911283f

Please sign in to comment.