利用openssl生成公钥、私钥 Rsa加密、解密及验证签名

 

//获取公钥私钥

X509Certificate2 c4 = DataCertificate.GetCertFromCerFile(path + "\\cer\\xx.pem");

string PublicKey = c4.PublicKey.Key.ToXmlString(false);//公钥
X509Certificate2 c3 = DataCertificate.GetCertificateFromPfxFile(path + "\\cer\\yy.pfx", "密码");
string PrivateKey = c3.PrivateKey.ToXmlString(true);//私钥

SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();

sParaTemp.Add("字段名", “内容”);
string sParaTempjson = JsonConvert.SerializeObject(sParaTemp);
string paydata = Program.RSAEncrypt(PublicKey, sParaTempjson); // 加密

backpaydata = Program.RSADecrypt(PrivateKey, backpaydata.Replace("\\", "")); // 解密

//Program.cs文件  加密解密 签名代码

/// <summary>
/// RSA加密 要加密较长的数据,则可以采用分段加解密的方式
/// </summary>
/// <param name="xmlPublicKey"></param>
/// <param name="m_strEncryptString"></param>
/// <returns></returns>

public static string RSAEncrypt(string xmlPublicKey, string plaintext)
{
X509Certificate2 _X509Certificate2 = RetrieveX509Certificate();

using (RSACryptoServiceProvider RSACryptography =new RSACryptoServiceProvider())
{
RSACryptography.FromXmlString(xmlPublicKey);
Byte[] PlaintextData = Encoder.GetBytes(plaintext);
int MaxBlockSize = RSACryptography.KeySize / 8 - 11; //加密块最大长度限制

if (PlaintextData.Length <= MaxBlockSize)
return Convert.ToBase64String(RSACryptography.Encrypt(PlaintextData, false));

using (MemoryStream PlaiStream = new MemoryStream(PlaintextData))
using (MemoryStream CrypStream = new MemoryStream())
{
Byte[] Buffer = new Byte[MaxBlockSize];
int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);

while (BlockSize > 0)
{
Byte[] ToEncrypt = new Byte[BlockSize];
Array.Copy(Buffer, 0, ToEncrypt, 0, BlockSize);

Byte[] Cryptograph = RSACryptography.Encrypt(ToEncrypt, false);
CrypStream.Write(Cryptograph, 0, Cryptograph.Length);

BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
}

return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None);
}
}
}

public static string RSADecrypt(string xmlPrivateKey, string ciphertext)
{
X509Certificate2 _X509Certificate2 = RetrieveX509Certificate();
using (RSACryptoServiceProvider RSACryptography = new RSACryptoServiceProvider())
{
RSACryptography.FromXmlString(xmlPrivateKey);

Byte[] CiphertextData = Convert.FromBase64String(ciphertext);
int MaxBlockSize = RSACryptography.KeySize / 8; //解密块最大长度限制

if (CiphertextData.Length <= MaxBlockSize)
return Encoder.GetString(RSACryptography.Decrypt(CiphertextData, false));

using (MemoryStream CrypStream = new MemoryStream(CiphertextData))
using (MemoryStream PlaiStream = new MemoryStream())
{
Byte[] Buffer = new Byte[MaxBlockSize];
int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);

while (BlockSize > 0)
{
Byte[] ToDecrypt = new Byte[BlockSize];
Array.Copy(Buffer, 0, ToDecrypt, 0, BlockSize);

Byte[] Plaintext = RSACryptography.Decrypt(ToDecrypt, false);
PlaiStream.Write(Plaintext, 0, Plaintext.Length);

BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
}

return Encoder.GetString(PlaiStream.ToArray());
}
}
}

 

 

//签名部分 根据实际情况可自行选择调用方法

#region RSA数字签名

#region 获取Hash描述表
/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="strSource">待签名的字符串</param>
/// <param name="HashData">Hash描述</param>
/// <returns></returns>
public bool GetHash(string strSource, ref byte[] HashData)
{
try
{
byte[] Buffer;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);
HashData = MD5.ComputeHash(Buffer);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="strSource">待签名的字符串</param>
/// <param name="strHashData">Hash描述</param>
/// <returns></returns>
public static bool GetHash(string strSource, ref string strHashData)
{
try
{
//从字符串中取得Hash描述
byte[] Buffer;
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("sha1");
Buffer = System.Text.Encoding.GetEncoding("utf-8").GetBytes(strSource);
HashData = MD5.ComputeHash(Buffer);
strHashData = Convert.ToBase64String(HashData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="objFile">待签名的文件</param>
/// <param name="HashData">Hash描述</param>
/// <returns></returns>
public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
{
try
{
//从文件中取得Hash描述
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="objFile">待签名的文件</param>
/// <param name="strHashData">Hash描述</param>
/// <returns></returns>
public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
{
try
{
//从文件中取得Hash描述
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();
strHashData = Convert.ToBase64String(HashData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region RSA签名
/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="HashbyteSignature">待签名Hash描述</param>
/// <param name="EncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="HashbyteSignature">待签名Hash描述</param>
/// <param name="m_strEncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref string strEncryptedSignatureData)
{
try
{
byte[] EncryptedSignatureData;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="strHashbyteSignature">待签名Hash描述</param>
/// <param name="EncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
public bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
byte[] HashbyteSignature;

HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="strHashbyteSignature">待签名Hash描述</param>
/// <param name="strEncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
public static bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref string strEncryptedSignatureData)
{


try
{

byte[] HashbyteSignature;
byte[] EncryptedSignatureData;
HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("sha1");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region RSA 签名验证
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="HashbyteDeformatter">Hash描述</param>
/// <param name="DeformatterData">签名后的结果</param>
/// <returns></returns>
public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="strHashbyteDeformatter">Hash描述</param>
/// <param name="DeformatterData">签名后的结果</param>
/// <returns></returns>
public bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, byte[] DeformatterData)
{
try
{
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="HashbyteDeformatter">Hash描述</param>
/// <param name="strDeformatterData">签名后的结果</param>
/// <returns></returns>
public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, string strDeformatterData)
{
try
{
byte[] DeformatterData;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
DeformatterData = Convert.FromBase64String(strDeformatterData);
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="strHashbyteDeformatter">Hash描述</param>
/// <param name="strDeformatterData">签名后的结果</param>
/// <returns></returns>
public static bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, string strDeformatterData)
{
try
{
byte[] DeformatterData;
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("sha1");
DeformatterData = Convert.FromBase64String(strDeformatterData);
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#endregion

 

posted @ 2015-04-30 16:07  沙加+  阅读(2952)  评论(0编辑  收藏  举报