RsaHelper工具类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApplication1
{
public class RsaHelper
{
#region 加密
/// <summary>
/// 加密
/// </summary>
/// <param name="encryptString">代签名字符串</param>
/// <param name="publicKey">证书路径</param>
/// <returns></returns>
public static string RsaEncrypt(string encryptString, string publicKey)
{
string result;
if (string.IsNullOrEmpty(encryptString))
{
result = string.Empty;
}
else
{
if (string.IsNullOrWhiteSpace(publicKey))
{
throw new ArgumentException("Invalid Public Key");
}
using (RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider())
{
byte[] bytes = Encoding.UTF8.GetBytes(encryptString);
RSACryptoServiceProvider publicKey2 = GetPublicKey(publicKey);
rSACryptoServiceProvider.FromXmlString(publicKey2.ToXmlString(false));
int num = rSACryptoServiceProvider.KeySize / 8 - 11;
byte[] array = new byte[num];
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
using (MemoryStream memoryStream2 = new MemoryStream())
{
while (true)
{
int num2 = memoryStream.Read(array, 0, num);
if (num2 <= 0)
{
break;
}
byte[] array2 = new byte[num2];
Array.Copy(array, 0, array2, 0, num2);
byte[] array3 = rSACryptoServiceProvider.Encrypt(array2, false);
memoryStream2.Write(array3, 0, array3.Length);
}
result = Convert.ToBase64String(memoryStream2.ToArray());
}
}
}
}
return result;
}
#endregion
#region 得到公钥
private static RSACryptoServiceProvider GetPublicKey(string pubKeyFile)
{
X509Certificate2 x509Certificate = new X509Certificate2(pubKeyFile);
return (RSACryptoServiceProvider)x509Certificate.PublicKey.Key;
}
#endregion
#region 解密
/// <summary>
/// 解密
/// </summary>
/// <param name="decryptString">待机密字符串</param>
/// <param name="privateKey">公钥文件路径</param>
/// <param name="pwd">公钥密码</param>
/// <returns></returns>
public static string RsaDecrypt(string decryptString, string privateKey, string pwd)
{
string result;
if (string.IsNullOrEmpty(decryptString))
{
result = null;
}
else
{
if (string.IsNullOrWhiteSpace(privateKey))
{
throw new ArgumentException("Invalid Private Key");
}
using (RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider())
{
byte[] buffer = Convert.FromBase64String(decryptString);
RSACryptoServiceProvider privateKey2 = GetPrivateKey(privateKey, pwd);
rSACryptoServiceProvider.FromXmlString(privateKey2.ToXmlString(true));
int num = rSACryptoServiceProvider.KeySize / 8;
byte[] array = new byte[num];
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
using (MemoryStream memoryStream2 = new MemoryStream())
{
while (true)
{
int num2 = memoryStream.Read(array, 0, num);
if (num2 <= 0)
{
break;
}
byte[] array2 = new byte[num2];
Array.Copy(array, 0, array2, 0, num2);
byte[] array3 = rSACryptoServiceProvider.Decrypt(array2, false);
memoryStream2.Write(array3, 0, array3.Length);
}
result = Encoding.UTF8.GetString(memoryStream2.ToArray());
}
}
}
}
return result;
}
#endregion
#region 获取私钥
public static RSACryptoServiceProvider GetPrivateKey(string priKeyFile, string keyPwd)
{
X509Certificate2 x509Certificate = new X509Certificate2(priKeyFile, keyPwd, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
return (RSACryptoServiceProvider)x509Certificate.PrivateKey;
}
#endregion
#region 私钥得到签名
/// <summary>
/// 私钥得到签名
/// </summary>
/// <param name="data">签名字符串</param>
/// <param name="privateKey">私钥路径</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public static string Sign(string data, string privateKey, string pwd)
{
string Sign = "";
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] msg = sha1.ComputeHash(Encoding.GetEncoding("UTF-8").GetBytes(data));
RSAPKCS1SignatureFormatter signe = new RSAPKCS1SignatureFormatter();
X509Certificate2 HYRZZScerSigneCert = new X509Certificate2(privateKey, pwd);
signe.SetKey(HYRZZScerSigneCert.PrivateKey);
signe.SetHashAlgorithm("SHA1");
Sign = Convert.ToBase64String(signe.CreateSignature(msg));
return Sign;
}
#endregion
#region 公钥验签
/// <summary>
/// 公钥验签
/// </summary>
/// <param name="data">验签数据</param>
/// <param name="signeText">待验签的签名</param>
/// <param name="publicKey">公钥路径</param>
/// <returns></returns>
public static bool VerifySign(string data, string signeText, string publicKey)
{
bool bl = false;
X509Certificate2 HYRZZScerVerifyCert = new X509Certificate2(publicKey);
byte[] signe = Convert.FromBase64String(signeText);
bl = ((RSACryptoServiceProvider)HYRZZScerVerifyCert.PublicKey.Key).VerifyData(
Encoding.GetEncoding("UTF-8").GetBytes(data),
"SHA1",
signe);
return bl;
}
#endregion
}
}
公钥加密,私钥解密调用:
string keyStr = "123456789TTTAAA6";
Console.WriteLine("明文:" + keyStr);
string MiWen = RsaHelper.RsaEncrypt(keyStr, @"D:\Program Files (x86)\Kingdee\K3Cloud\WebSite\cer\wts.cer");
Console.WriteLine("密文:" + MiWen);
string MingWen = RsaHelper.RsaDecrypt(MiWen, @"D:\Program Files (x86)\Kingdee\K3Cloud\WebSite\cer\wts-111111.pfx", "111111");
Console.WriteLine("明文:" + MingWen);
私钥得到签名,公钥验签:
string data = "123654789";
string signData = RsaHelper.Sign(data, @"D:\Program Files (x86)\Kingdee\K3Cloud\WebSite\cer\wts-111111.pfx", "111111");
Console.WriteLine("signData:" + signData);
bool b = RsaHelper.VerifySign(data, signData, @"D:\Program Files (x86)\Kingdee\K3Cloud\WebSite\cer\wts.cer");
Console.WriteLine("验签:" + b);