上代码,帮助类
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
class RsaHelper
{
/// <summary>
/// 用私钥给资料进行RSA加密
/// </summary>
/// <param name=”xmlPrivateKey”> 私钥(XML格式字串)</param>
/// <param name=”strEncryptString”> 要加密的资料 </param>
/// <param name=”Encryption”> 是否加密,true加密反之解密</param>
/// <returns> 加密后的资料 </returns>
public static string PrivateKeyEncrypt(string xmlPrivateKey, string strEncryptString,bool Encryption)
{
RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider();
privateRsa.FromXmlString(xmlPrivateKey);
AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa);
IBufferedCipher c = CipherUtilities.GetCipher(“RSA/ECB/PKCS1Padding”); //使用RSA/ECB/PKCS1Padding格式
c.Init(Encryption, keyPair.Private);//true表示加密,false表示解密
byte[] DataToEncrypt = Encoding.UTF8.GetBytes(strEncryptString);
byte[] outBytes = c.DoFinal(DataToEncrypt);//加密
string strBase64 = Convert.ToBase64String(outBytes);
return strBase64;
}
/// <summary>
/// 用公钥给资料进行RSA解密
/// </summary>
/// <param name=”xmlPublicKey”> 公钥(XML格式字串) </param>
/// <param name=”strDecryptString”> 要解密资料 </param>
/// <param name=”Encryption”> 是否加密,true加密反之解密</param>
/// <returns> 解密后的资料 </returns>
public static string PublicKeyDecrypt(string xmlPublicKey, string strDecryptString,bool Encryption)
{
RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider();
publicRsa.FromXmlString(xmlPublicKey);
RSAParameters rp = publicRsa.ExportParameters(false);
AsymmetricKeyParameter pbk = DotNetUtilities.GetRsaPublicKey(rp);
IBufferedCipher c = CipherUtilities.GetCipher(“RSA/ECB/PKCS1Padding”);//使用RSA/ECB/PKCS1Padding格式
c.Init(Encryption, pbk);//true表示加密,false表示解密
byte[] DataToDecrypt = Convert.FromBase64String(strDecryptString);
byte[] outBytes = c.DoFinal(DataToDecrypt);//解密
string strDec = Encoding.UTF8.GetString(outBytes);
return strDec;
}
/// <summary>
/// XML公钥转成Pem公钥
/// </summary>
/// <param name=”xmlPublicKey”></param>
/// <returns></returns>
public static string XmlPublicKeyToPem(string xmlPublicKey)
{
RSAParameters rsaParam;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPublicKey);
rsaParam = rsa.ExportParameters(false);
}
RsaKeyParameters param = new RsaKeyParameters(false, new BigInteger(1, rsaParam.Modulus), new BigInteger(1, rsaParam.Exponent));
string pemPublicKeyStr = null;
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms))
{
var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
pemWriter.WriteObject(param);
sw.Flush();
byte[] buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, (int)ms.Length);
pemPublicKeyStr = Encoding.UTF8.GetString(buffer);
}
}
return pemPublicKeyStr;
}
/// <summary>
/// Pem公钥转成XML公钥
/// </summary>
/// <param name=”pemPublicKeyStr”></param>
/// <returns></returns>
public static string PemPublicKeyToXml(string pemPublicKeyStr)
{
RsaKeyParameters pemPublicKey;
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(pemPublicKeyStr)))
{
using (var sr = new StreamReader(ms))
{
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(sr);
pemPublicKey = (RsaKeyParameters)pemReader.ReadObject();
}
}
var p = new RSAParameters
{
Modulus = pemPublicKey.Modulus.ToByteArrayUnsigned(),
Exponent = pemPublicKey.Exponent.ToByteArrayUnsigned()
};
string xmlPublicKeyStr;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(p);
xmlPublicKeyStr = rsa.ToXmlString(false);
}
return xmlPublicKeyStr;
}
/// <summary>
/// XML私钥转成PEM私钥
/// </summary>
/// <param name=”xmlPrivateKey”></param>
/// <returns></returns>
public static string XmlPrivateKeyToPem(string xmlPrivateKey)
{
RSAParameters rsaParam;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPrivateKey);
rsaParam = rsa.ExportParameters(true);
}
var param = new RsaPrivateCrtKeyParameters(
new BigInteger(1, rsaParam.Modulus), new BigInteger(1, rsaParam.Exponent), new BigInteger(1, rsaParam.D),
new BigInteger(1, rsaParam.P), new BigInteger(1, rsaParam.Q), new BigInteger(1, rsaParam.DP), new BigInteger(1, rsaParam.DQ),
new BigInteger(1, rsaParam.InverseQ));
string pemPrivateKeyStr = null;
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms))
{
var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
pemWriter.WriteObject(param);
sw.Flush();
byte[] buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, (int)ms.Length);
pemPrivateKeyStr = Encoding.UTF8.GetString(buffer);
}
}
return pemPrivateKeyStr;
}
/// <summary>
/// Pem私钥转成XML私钥
/// </summary>
/// <param name=”pemPrivateKeyStr”></param>
/// <returns></returns>
public static string PemPrivateKeyToXml(string pemPrivateKeyStr)
{
RsaPrivateCrtKeyParameters pemPrivateKey;
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(pemPrivateKeyStr)))
{
using (var sr = new StreamReader(ms))
{
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(sr);
var keyPair = (AsymmetricCipherKeyPair)pemReader.ReadObject();
pemPrivateKey = (RsaPrivateCrtKeyParameters)keyPair.Private;
}
}
var p = new RSAParameters
{
Modulus = pemPrivateKey.Modulus.ToByteArrayUnsigned(),
Exponent = pemPrivateKey.PublicExponent.ToByteArrayUnsigned(),
D = pemPrivateKey.Exponent.ToByteArrayUnsigned(),
P = pemPrivateKey.P.ToByteArrayUnsigned(),
Q = pemPrivateKey.Q.ToByteArrayUnsigned(),
DP = pemPrivateKey.DP.ToByteArrayUnsigned(),
DQ = pemPrivateKey.DQ.ToByteArrayUnsigned(),
InverseQ = pemPrivateKey.QInv.ToByteArrayUnsigned(),
};
string xmlPrivateKeyStr;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(p);
xmlPrivateKeyStr = rsa.ToXmlString(true);
}
return xmlPrivateKeyStr;
}
}
调用
class Program
{
public static string PrivateKey = @”—–BEGIN RSA PRIVATE KEY—–
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx
…………..
—–END RSA PRIVATE KEY—–“;
public static string PublicKey = @”—–BEGIN CERTIFICATE—–
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx
…………..
—–END CERTIFICATE—–“;
static void Main(string[] args)
{
string res = RsaHelper.PemPrivateKeyToXml(PrivateKey);
res = RsaHelper.PrivateKeyEncrypt(res, “1”,true);
Console.WriteLine(res);
string res1 = RsaHelper.PemPublicKeyToXml(PublicKey );
res1 = RsaHelper.PublicKeyDecrypt(res1, res ,false);
Console.WriteLine(res);
Console.ReadKey();
}
}
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
一颗大萝北
本文地址: C# RSA私钥加密,公钥解密、公钥加密,私钥解密
本文地址: C# RSA私钥加密,公钥解密、公钥加密,私钥解密