using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.Xml.Linq; namespace Payment.LianLianPay.Sdk.Infrastructure
{
public static class RsaHelper
{
/// <summary>
/// private key sign
/// </summary>
/// <param name="preSign"></param>
/// <param name="isJavaFormatKey"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string RsaSign(string preSign, bool isJavaFormatKey, string privateKey)
{
try
{
//// net
//var rsa = new RSACryptoServiceProvider();
//rsa.FromXmlString(privateKey);
//byte[] signBytes = rsa.SignData(UTF8Encoding.UTF8.GetBytes(signStr), "md5");
//return Convert.ToBase64String(signBytes); // net core 2.0
using (var rsa = RSA.Create())
{
if (isJavaFormatKey) privateKey = RsaPrivateKeyJava2DotNet(privateKey);
rsa.FromXmlStringExtensions(privateKey);
var bytes = rsa.SignData(Encoding.UTF8.GetBytes(preSign), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(bytes);
}
}
catch (Exception e)
{
throw new ArgumentException(e.Message);
}
}
/// <summary>
/// .net => java Md5WithRsa
/// </summary>
/// <param name="preSign"></param>
/// <param name="isJavaFormatKey"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string RsaSignMd5WithRsa(string preSign, bool isJavaFormatKey, string privateKey)
{
try
{
//// net
//var rsa = new RSACryptoServiceProvider();
//rsa.FromXmlString(privateKey);
//byte[] signBytes = rsa.SignData(UTF8Encoding.UTF8.GetBytes(signStr), "md5");
//return Convert.ToBase64String(signBytes); // net core 2.0
using (var rsa = RSA.Create())
{
if (isJavaFormatKey) privateKey = RsaPrivateKeyJava2DotNet(privateKey);
rsa.FromXmlStringExtensions(privateKey);
var bytes = rsa.SignData(Encoding.UTF8.GetBytes(preSign), HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(bytes);
}
}
catch (Exception e)
{
throw new ArgumentException(e.Message);
}
}
/// <summary>
/// public key sign
/// </summary>
/// <param name="preSign"></param>
/// <param name="publicKey"></param>
/// <returns></returns>
public static string RsaEncrypt(string preSign, string publicKey)
{
try
{
//// net
//RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//rsa.FromXmlString(strPublicKey);
//byte[] bytes = rsa.Encrypt(UTF8Encoding.UTF8.GetBytes(strEncryptInfo), false);
//return Convert.ToBase64String(bytes); // net core 2.0
using (var rsa = RSA.Create())
{
publicKey = RsaPublicKeyJava2DotNet(publicKey);
rsa.FromXmlStringExtensions(publicKey);
var bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(preSign), RSAEncryptionPadding.Pkcs1);
return Convert.ToBase64String(bytes);
}
}
catch (Exception e)
{
throw new ArgumentException(e.Message);
}
}
/// <summary>
/// public key validate
/// </summary>
/// <param name="preSign"></param>
/// <param name="isJavaFormatKey"></param>
/// <param name="publicKey"></param>
/// <param name="signedData"></param>
/// <returns></returns>
public static bool ValidateRsaSign(string preSign, bool isJavaFormatKey, string publicKey, string signedData)
{
try
{
//// net
//RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//rsa.FromXmlString(publicKey);
//return rsa.VerifyData(UTF8Encoding.UTF8.GetBytes(plainText), "md5", Convert.FromBase64String(signedData)); // net core 2.0
using (var rsa = RSA.Create())
{
if (isJavaFormatKey) publicKey = RsaPublicKeyJava2DotNet(publicKey);
rsa.FromXmlStringExtensions(publicKey);
return rsa.VerifyData(Encoding.UTF8.GetBytes(preSign), Convert.FromBase64String(signedData), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
}
catch (Exception e)
{
throw new ArgumentException(e.Message);
}
}
/// <summary>
/// private key ,java->.net
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string RsaPrivateKeyJava2DotNet(string privateKey)
{
if (string.IsNullOrEmpty(privateKey))
{
return string.Empty;
}
var privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
return
$"<RSAKeyValue><Modulus>{Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned())}</Modulus><Exponent>{Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned())}</Exponent><P>{Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned())}</P><Q>{Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned())}</Q><DP>{Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned())}</DP><DQ>{Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned())}</DQ><InverseQ>{Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned())}</InverseQ><D>{Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned())}</D></RSAKeyValue>";
}
/// <summary>
/// public key ,java->.net
/// </summary>
/// <param name="publicKey"></param>
/// <returns>格式转换结果</returns>
public static string RsaPublicKeyJava2DotNet(string publicKey)
{
if (string.IsNullOrEmpty(publicKey))
{
return string.Empty;
} var publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
return string.Format(
"<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())
);
}
/// <summary>
/// private key ,.net->java
/// </summary>
/// <param name="privateKey">.net生成的私钥</param>
/// <returns></returns>
public static string RsaPrivateKeyDotNet2Java(string privateKey)
{
var doc = new XmlDocument();
doc.LoadXml(privateKey);
var m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
var exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
var d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
var p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
var q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
var dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
var dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
var qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText)); var privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv); var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
var serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
return Convert.ToBase64String(serializedPrivateBytes);
}
/// <summary>
/// public key ,.net->java
/// </summary>
/// <param name="publicKey">.net生成的公钥</param>
/// <returns></returns>
public static string RsaPublicKeyDotNet2Java(string publicKey)
{
var doc = new XmlDocument();
doc.LoadXml(publicKey);
var m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
var p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
var pub = new RsaKeyParameters(false, m, p); var publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
var serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
return Convert.ToBase64String(serializedPublicBytes);
}
// 处理 下面两种方式都会出现的 Operation is not supported on this platform 异常
// RSA.Create().FromXmlString(privateKey)
// new RSACryptoServiceProvider().FromXmlString(privateKey)
/// <summary>
/// 扩展FromXmlString
/// </summary>
/// <param name="rsa"></param>
/// <param name="xmlString"></param>
private static void FromXmlStringExtensions(this RSA rsa, string xmlString)
{
var parameters = new RSAParameters(); var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString); if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
{
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{
switch (node.Name)
{
case "Modulus":
parameters.Modulus = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
case "Exponent":
parameters.Exponent = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
case "P":
parameters.P = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
case "Q":
parameters.Q = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
case "DP":
parameters.DP = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
case "DQ":
parameters.DQ = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
case "InverseQ":
parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
case "D":
parameters.D = (string.IsNullOrEmpty(node.InnerText)
? null
: Convert.FromBase64String(node.InnerText));
break;
}
}
}
else
{
throw new Exception("Invalid XML RSA key.");
} rsa.ImportParameters(parameters);
}
// /// <summary>
// /// 扩展ToXmlString,创建公私钥
// /// </summary>
// /// <param name="includePrivateParameters"></param>
// /// <returns></returns>
// public static string ToCreateKey(bool includePrivateParameters)
// {
// using (var rsa = RSA.Create())
// {
// var parameters = rsa.ExportParameters(includePrivateParameters);
// return
// $"<RSAKeyValue><Modulus>{(parameters.Modulus != null ? Convert.ToBase64String(parameters.Modulus) : null)}</Modulus><Exponent>{(parameters.Exponent != null ? Convert.ToBase64String(parameters.Exponent) : null)}</Exponent><P>{(parameters.P != null ? Convert.ToBase64String(parameters.P) : null)}</P><Q>{(parameters.Q != null ? Convert.ToBase64String(parameters.Q) : null)}</Q><DP>{(parameters.DP != null ? Convert.ToBase64String(parameters.DP) : null)}</DP><DQ>{(parameters.DQ != null ? Convert.ToBase64String(parameters.DQ) : null)}</DQ><InverseQ>{(parameters.InverseQ != null ? Convert.ToBase64String(parameters.InverseQ) : null)}</InverseQ><D>{(parameters.D != null ? Convert.ToBase64String(parameters.D) : null)}</D></RSAKeyValue>";
// }
// }
/// <summary>
/// Generate XML Format RSA Key. Result: Index 0 is the private key and index 1 is the public key
/// </summary>
/// <param name="keySize">Key Size.Unit: bits</param>
/// <returns></returns>
public static List<string> XmlKey(int keySize)
{
using (var rsa = RSA.Create())
{
rsa.KeySize = keySize;
var rsap = rsa.ExportParameters(true);
var res = new List<string>();
var privatElement = new XElement("RSAKeyValue");
//Modulus
var primodulus = new XElement("Modulus", Convert.ToBase64String(rsap.Modulus));
//Exponent
var priexponent = new XElement("Exponent", Convert.ToBase64String(rsap.Exponent));
//P
var prip = new XElement("P", Convert.ToBase64String(rsap.P));
//Q
var priq = new XElement("Q", Convert.ToBase64String(rsap.Q));
//DP
var pridp = new XElement("DP", Convert.ToBase64String(rsap.DP));
//DQ
var pridq = new XElement("DQ", Convert.ToBase64String(rsap.DQ));
//InverseQ
var priinverseQ = new XElement("InverseQ", Convert.ToBase64String(rsap.InverseQ));
//D
var prid = new XElement("D", Convert.ToBase64String(rsap.D)); privatElement.Add(primodulus);
privatElement.Add(priexponent);
privatElement.Add(prip);
privatElement.Add(priq);
privatElement.Add(pridp);
privatElement.Add(pridq);
privatElement.Add(priinverseQ);
privatElement.Add(prid); //添加私钥
res.Add(privatElement.ToString()); var publicElement = new XElement("RSAKeyValue");
//Modulus
var pubmodulus = new XElement("Modulus", Convert.ToBase64String(rsap.Modulus));
//Exponent
var pubexponent = new XElement("Exponent", Convert.ToBase64String(rsap.Exponent)); publicElement.Add(pubmodulus);
publicElement.Add(pubexponent); //添加公钥
res.Add(publicElement.ToString()); return res;
}
}
/// <summary>
/// Generate RSA key in Pkcs1 format. Result: Index 0 is the private key and index 1 is the public key
/// </summary>
/// <param name="keySize">Key Size.Unit: bits</param>
/// <param name="format">Whether the format is true If it is standard pem file format</param>
/// <returns></returns>
public static List<string> Pkcs1Key(int keySize, bool format = true)
{
var res = new List<string>(); var kpGen = GeneratorUtilities.GetKeyPairGenerator("RSA");
kpGen.Init(new KeyGenerationParameters(new SecureRandom(), keySize));
var keyPair = kpGen.GenerateKeyPair(); var sw = new StringWriter();
var pWrt = new PemWriter(sw);
pWrt.WriteObject(keyPair.Private);
pWrt.Writer.Flush();
var privateKey = sw.ToString(); if (!format)
{
privateKey = privateKey.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("\r\n", "");
} res.Add(privateKey); var swpub = new StringWriter();
var pWrtpub = new PemWriter(swpub);
pWrtpub.WriteObject(keyPair.Public);
pWrtpub.Writer.Flush();
var publicKey = swpub.ToString();
if (!format)
{
publicKey = publicKey.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace("\r\n", "");
} res.Add(publicKey); return res;
}
/// <summary>
/// Generate Pkcs8 format RSA key. Result: Index 0 is the private key and index 1 is the public key
/// </summary>
/// <param name="keySize">Key Size.Unit: bits</param>
/// <param name="format">Whether the format is true If it is standard pem file format</param>
/// <returns></returns>
public static List<string> Pkcs8Key(int keySize, bool format = true)
{
var res = new List<string>(); var kpGen = GeneratorUtilities.GetKeyPairGenerator("RSA");
kpGen.Init(new KeyGenerationParameters(new SecureRandom(), keySize));
var keyPair = kpGen.GenerateKeyPair(); var swpri = new StringWriter();
var pWrtpri = new PemWriter(swpri);
var pkcs8 = new Pkcs8Generator(keyPair.Private);
pWrtpri.WriteObject(pkcs8);
pWrtpri.Writer.Flush();
var privateKey = swpri.ToString(); if (!format)
{
privateKey = privateKey.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\r\n", "");
} res.Add(privateKey); var swpub = new StringWriter();
var pWrtpub = new PemWriter(swpub);
pWrtpub.WriteObject(keyPair.Public);
pWrtpub.Writer.Flush();
var publicKey = swpub.ToString();
if (!format)
{
publicKey = publicKey.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace("\r\n", "");
} res.Add(publicKey); return res;
}
/// <summary>
/// Public Key Convert xml->xml
/// </summary>
/// <param name="publicKey"></param>
/// <returns></returns>
public static string PublicKeyXmlToPem(string publicKey)
{
var root = XElement.Parse(publicKey);
//Modulus
var modulus = root.Element("Modulus");
//Exponent
var exponent = root.Element("Exponent"); var rsaKeyParameters = new RsaKeyParameters(false, new BigInteger(1, Convert.FromBase64String(modulus?.Value)), new BigInteger(1, Convert.FromBase64String(exponent?.Value))); var sw = new StringWriter();
var pWrt = new PemWriter(sw);
pWrt.WriteObject(rsaKeyParameters);
pWrt.Writer.Flush();
return sw.ToString();
}
/// <summary>
/// Private Key Convert xml->Pkcs1
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string PrivateKeyXmlToPkcs1(string privateKey)
{
var root = XElement.Parse(privateKey);
//Modulus
var modulus = root.Element("Modulus");
//Exponent
var exponent = root.Element("Exponent");
//P
var p = root.Element("P");
//Q
var q = root.Element("Q");
//DP
var dp = root.Element("DP");
//DQ
var dq = root.Element("DQ");
//InverseQ
var inverseQ = root.Element("InverseQ");
//D
var d = root.Element("D"); var rsaPrivateCrtKeyParameters = new RsaPrivateCrtKeyParameters(
new BigInteger(1, Convert.FromBase64String(modulus?.Value)),
new BigInteger(1, Convert.FromBase64String(exponent?.Value)),
new BigInteger(1, Convert.FromBase64String(d?.Value)),
new BigInteger(1, Convert.FromBase64String(p?.Value)),
new BigInteger(1, Convert.FromBase64String(q?.Value)),
new BigInteger(1, Convert.FromBase64String(dp?.Value)),
new BigInteger(1, Convert.FromBase64String(dq?.Value)),
new BigInteger(1, Convert.FromBase64String(inverseQ?.Value))); var sw = new StringWriter();
var pWrt = new PemWriter(sw);
pWrt.WriteObject(rsaPrivateCrtKeyParameters);
pWrt.Writer.Flush();
return sw.ToString(); }
/// <summary>
/// Format public key
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string PublicKeyFormat(string str)
{
if (str.StartsWith("-----BEGIN PUBLIC KEY-----"))
{
return str;
}
var res = new List<string> { "-----BEGIN PUBLIC KEY-----" };
var pos = 0;
while (pos < str.Length)
{
var count = str.Length - pos < 64 ? str.Length - pos : 64;
res.Add(str.Substring(pos, count));
pos += count;
}
res.Add("-----END PUBLIC KEY-----");
var resStr = string.Join("\r\n", res);
return resStr;
}
/// <summary>
/// Format Pkcs8 format private key
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string Pkcs8PrivateKeyFormat(string str)
{
if (str.StartsWith("-----BEGIN PRIVATE KEY-----"))
{
return str;
}
var res = new List<string> { "-----BEGIN PRIVATE KEY-----" };
var pos = 0;
while (pos < str.Length)
{
var count = str.Length - pos < 64 ? str.Length - pos : 64;
res.Add(str.Substring(pos, count));
pos += count;
}
res.Add("-----END PRIVATE KEY-----");
var resStr = string.Join("\r\n", res);
return resStr;
}
/// <summary>
/// Format Pkcs1 format private key
/// Author:Zhiqiang Li
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string Pkcs1PrivateKeyFormat(string str)
{
if (str.StartsWith("-----BEGIN RSA PRIVATE KEY-----"))
{
return str;
}
var res = new List<string> { "-----BEGIN RSA PRIVATE KEY-----" };
var pos = 0;
while (pos < str.Length)
{
var count = str.Length - pos < 64 ? str.Length - pos : 64;
res.Add(str.Substring(pos, count));
pos += count;
}
res.Add("-----END RSA PRIVATE KEY-----");
var resStr = string.Join("\r\n", res);
return resStr;
}
/// <summary>
/// Remove the Pkcs8 format private key format
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string Pkcs8PrivateKeyFormatRemove(string str)
{
if (!str.StartsWith("-----BEGIN PRIVATE KEY-----"))
{
return str;
}
return str.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "")
.Replace("\r\n", "");
}
/// <summary>
/// public Key pem to xml
/// </summary>
/// <param name="publicKey"></param>
/// <returns></returns>
public static string PublicKeyPemToXml(string publicKey)
{
publicKey = PublicKeyFormat(publicKey); var pr = new PemReader(new StringReader(publicKey));
var obj = pr.ReadObject();
if (!(obj is RsaKeyParameters rsaKey))
{
throw new Exception("Public key format is incorrect");
} var publicElement = new XElement("RSAKeyValue");
//Modulus
var pubmodulus = new XElement("Modulus", Convert.ToBase64String(rsaKey.Modulus.ToByteArrayUnsigned()));
//Exponent
var pubexponent = new XElement("Exponent", Convert.ToBase64String(rsaKey.Exponent.ToByteArrayUnsigned())); publicElement.Add(pubmodulus);
publicElement.Add(pubexponent);
return publicElement.ToString();
}
/// <summary>
/// Private Key Convert Pkcs1->xml
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string PrivateKeyPkcs1ToXml(string privateKey)
{
privateKey = Pkcs1PrivateKeyFormat(privateKey); var pr = new PemReader(new StringReader(privateKey));
if (!(pr.ReadObject() is AsymmetricCipherKeyPair asymmetricCipherKeyPair))
{
throw new Exception("Private key format is incorrect");
}
var rsaPrivateCrtKeyParameters =
(RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(
PrivateKeyInfoFactory.CreatePrivateKeyInfo(asymmetricCipherKeyPair.Private)); var privatElement = new XElement("RSAKeyValue");
//Modulus
var primodulus = new XElement("Modulus", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Modulus.ToByteArrayUnsigned()));
//Exponent
var priexponent = new XElement("Exponent", Convert.ToBase64String(rsaPrivateCrtKeyParameters.PublicExponent.ToByteArrayUnsigned()));
//P
var prip = new XElement("P", Convert.ToBase64String(rsaPrivateCrtKeyParameters.P.ToByteArrayUnsigned()));
//Q
var priq = new XElement("Q", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Q.ToByteArrayUnsigned()));
//DP
var pridp = new XElement("DP", Convert.ToBase64String(rsaPrivateCrtKeyParameters.DP.ToByteArrayUnsigned()));
//DQ
var pridq = new XElement("DQ", Convert.ToBase64String(rsaPrivateCrtKeyParameters.DQ.ToByteArrayUnsigned()));
//InverseQ
var priinverseQ = new XElement("InverseQ", Convert.ToBase64String(rsaPrivateCrtKeyParameters.QInv.ToByteArrayUnsigned()));
//D
var prid = new XElement("D", Convert.ToBase64String(rsaPrivateCrtKeyParameters.Exponent.ToByteArrayUnsigned())); privatElement.Add(primodulus);
privatElement.Add(priexponent);
privatElement.Add(prip);
privatElement.Add(priq);
privatElement.Add(pridp);
privatElement.Add(pridq);
privatElement.Add(priinverseQ);
privatElement.Add(prid); return privatElement.ToString();
}
/// <summary>
/// Private Key Convert Pkcs8->xml
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string PrivateKeyPkcs8ToXml(string privateKey)
{
privateKey = Pkcs8PrivateKeyFormatRemove(privateKey);
var privateKeyParam =
(RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); var privatElement = new XElement("RSAKeyValue");
//Modulus
var primodulus = new XElement("Modulus", Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()));
//Exponent
var priexponent = new XElement("Exponent", Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()));
//P
var prip = new XElement("P", Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()));
//Q
var priq = new XElement("Q", Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()));
//DP
var pridp = new XElement("DP", Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()));
//DQ
var pridq = new XElement("DQ", Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()));
//InverseQ
var priinverseQ = new XElement("InverseQ", Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()));
//D
var prid = new XElement("D", Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned())); privatElement.Add(primodulus);
privatElement.Add(priexponent);
privatElement.Add(prip);
privatElement.Add(priq);
privatElement.Add(pridp);
privatElement.Add(pridq);
privatElement.Add(priinverseQ);
privatElement.Add(prid); return privatElement.ToString();
}
/// <summary>
/// Private Key Convert xml->Pkcs8
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string PrivateKeyXmlToPkcs8(string privateKey)
{
var root = XElement.Parse(privateKey);
//Modulus
var modulus = root.Element("Modulus");
//Exponent
var exponent = root.Element("Exponent");
//P
var p = root.Element("P");
//Q
var q = root.Element("Q");
//DP
var dp = root.Element("DP");
//DQ
var dq = root.Element("DQ");
//InverseQ
var inverseQ = root.Element("InverseQ");
//D
var d = root.Element("D"); var rsaPrivateCrtKeyParameters = new RsaPrivateCrtKeyParameters(
new BigInteger(1, Convert.FromBase64String(modulus?.Value)),
new BigInteger(1, Convert.FromBase64String(exponent?.Value)),
new BigInteger(1, Convert.FromBase64String(d?.Value)),
new BigInteger(1, Convert.FromBase64String(p?.Value)),
new BigInteger(1, Convert.FromBase64String(q?.Value)),
new BigInteger(1, Convert.FromBase64String(dp?.Value)),
new BigInteger(1, Convert.FromBase64String(dq?.Value)),
new BigInteger(1, Convert.FromBase64String(inverseQ?.Value))); var swpri = new StringWriter();
var pWrtpri = new PemWriter(swpri);
var pkcs8 = new Pkcs8Generator(rsaPrivateCrtKeyParameters);
pWrtpri.WriteObject(pkcs8);
pWrtpri.Writer.Flush();
return swpri.ToString(); }
/// <summary>
/// Private Key Convert Pkcs1->Pkcs8
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string PrivateKeyPkcs1ToPkcs8(string privateKey)
{
privateKey = Pkcs1PrivateKeyFormat(privateKey);
var pr = new PemReader(new StringReader(privateKey)); var kp = pr.ReadObject() as AsymmetricCipherKeyPair;
var sw = new StringWriter();
var pWrt = new PemWriter(sw);
var pkcs8 = new Pkcs8Generator(kp?.Private);
pWrt.WriteObject(pkcs8);
pWrt.Writer.Flush();
var result = sw.ToString();
return result;
}
/// <summary>
/// Private Key Convert Pkcs8->Pkcs1
/// </summary>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string PrivateKeyPkcs8ToPkcs1(string privateKey)
{
privateKey = Pkcs8PrivateKeyFormat(privateKey);
var pr = new PemReader(new StringReader(privateKey)); var kp = pr.ReadObject() as RsaPrivateCrtKeyParameters; var keyParameter = PrivateKeyFactory.CreateKey(PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp)); var sw = new StringWriter();
var pWrt = new PemWriter(sw);
pWrt.WriteObject(keyParameter);
pWrt.Writer.Flush();
var result = sw.ToString();
return result;
}
}
}

.net core 常用rsa 加签类的更多相关文章

  1. java RSA 加签验签【转】

    引用自: http://blog.csdn.net/wangqiuyun/article/details/42143957/ java RSA 加签验签 package com.testdemo.co ...

  2. RSA加密解密及RSA加签验签

    RSA安全性应用场景说明 在刚接触RSA的时候,会混淆RSA加密解密和RSA加签验签的概念.简单来说加密解密是公钥加密私钥解密,持有公钥(多人持有)可以对数据加密,但是只有持有私钥(一人持有)才可以解 ...

  3. 微信支付 V3 RSA 加签踩坑

    最近在做微信支付,根据微信官方文档上的要求 用RSA加签去请求支付窗口的调起,下面详细列举支付开发过程: 当前项目的流程大概是,前端根据后端要求提交数据------->拿到后台返回的prepay ...

  4. PHP RSA加签的实现过程

    一.得到私钥文件mycert.key 2.从pfx提取密钥信息,并转换为key格式(pfx使用pkcs12模式补足) (1)提取密钥对 openssl pkcs12 -in 1.pfx -nocert ...

  5. ruby中rsa加签解签方法

    # coding:utf-8require 'openssl'require 'base64'# rsa签名,文本内容和私钥路径def rsa_sign(data,private_key_path) ...

  6. ruby的加密方法整理(des rsa加密 加签)

    # coding:utf-8require 'openssl'require 'base64'#des加密并且base64编码def des_encrypt des_key, des_text des ...

  7. RSA加密解密与加签验签

    RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.1987年7月首次在美国公布 ...

  8. RSA加解密&RSA加验签详解

    RSA 加密算法是目前最有影响力的 公钥加密算法,并且被普遍认为是目前 最优秀的公钥方案 之一.RSA 是第一个能同时用于 加密 和 数字签名 的算法,它能够 抵抗 到目前为止已知的 所有密码攻击,已 ...

  9. RSA体系 c++/java相互进行加签验签--转

    在web开发中,采用RSA公钥密钥体系自制ukey,文件证书登陆时,普遍的做法为:在浏览器端采用c++ activex控件,使用 c++的第三库openssl进行RAS加签操作,在服务器端采用java ...

随机推荐

  1. 完美解决MSSQL安装问题“Polybase要求安装Oracle JRE 7更新51(64位)”方案

    阅文时长 | 0.72分钟 字数统计 | 1164.8字符 主要内容 | 1.问题起因及解决方案 2.安装jdk-8u241-windows-x64 3.取消PolyBase查询服务 4.四.声明与参 ...

  2. JAVA基础——运算符号

    运算符(java) 算数运算符:+,-,*,/,%(取余),++,-- 赋值运算符:= 关系运算符:<, >, >= ,<= ,== , != 逻辑运算符:&& ...

  3. [OS] 操作系统课程(五)

    系统启动 启动过程 CPU加电稳定后从0XFFFF0读取第一条指令 BIOS 固化到计算机主板上的程序 包括系统设置.自检程序和系统自启动程序 系统加电后读BIOS 加电自检POST,内存.显卡等关键 ...

  4. 其他CSS属性

    一.设置元素的颜色和透明度 a.color color 属性规定文本的颜色.这个属性设置了一个元素的前景色(在 HTML 表现中,就是元素文本的颜色):光栅图像不受 color 影响.这个颜色还会应用 ...

  5. Docker------阿里云部署私有镜像仓库

    Docker------阿里云部署私有镜像仓库   前言 公共镜像仓库 官方:https://hub.docker.com/ 基于各个软件开发或者软件提供方开发的 非官方:其它组织或公司开发的镜像,供 ...

  6. SSH自动断开连接的原因-20200323

    SSH自动断开连接的原因   方法一: 用putty/SecureCRT连续3分钟左右没有输入, 就自动断开, 然后必须重新登陆, 很麻烦. 在网上查了很多资料, 发现原因有多种, 环境变量TMOUT ...

  7. AD命令获取计算机、用户相关信息

    1. 获取AD用户相关信息(用户名.创建日期.最后修改密码日期.最后登录日期) Get AD users, Name/Created Date/Last change passwd Date/Last ...

  8. 云计算OpenStack共享组件---信息队列rabbitmq(2)

    一.MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 消息传 ...

  9. kvm虚拟化网络管理(5)

    一.Linux Bridge网桥管理 网络虚拟化是虚拟化技术中最复杂的部分,也是非常重要的资源. 第一节中我们创建了一个名为br0的linux-bridge网桥,如果在此网桥上新建一台vm,如下图: ...

  10. iowait 的常见误解

    转自:理解 %IOWAIT (%WIO):http://linuxperf.com/?p=33   %iowait 是 "sar -u" 等工具检查CPU使用率时显示的一个指标,在 ...