C# RSA加密和解密,RSA生成私钥和公钥(使用XML格式秘钥)
C# RSA加密和解密,RSA生成私钥和公钥(使用XML格式秘钥)
前言
网上有大量RSA加密解密解决方案,包括开源Nuget包和源码,作者测试过多种方案,最后选择本文介绍的方案,简单,代码少,效率高!
RsaHelper 类能够生成公钥和私钥,不需要在网上在线生成公钥和私钥啦!
优秀方案,推荐给大家!
生成xml格式的公钥和私钥
C# 全选
RsaHelper.GetXmlKey(out var privatekey1, out var publickey1);
PrivateKey
XML 全选
<RSAKeyValue><Modulus>0nMMhiGAklb2URWmQsxABYYdrVqyiXfd9kfEVgBsdb39kAw6ivdvOs4ahYWbutcQRq37qRqlRHHAguDxcTWmxpWsFmd9c5DIwvOyXrmynSgGdEVzu5NrCfZlLI7WwvFSQxF/3smdgXQxXYcYwdItV8dXd3CxBaqWS92OzMLkIr0=</Modulus><Exponent>AQAB</Exponent><P>60q62GF9otYbzo6o7Ux1ITNzdxzahBxw6uZ6f1ph+nu+dq/90DQlwxXHKA0T9Y2acw3NjQKg6QNdDvfbjO0Euw==</P><Q>5PiaMXTNBpCO0Yr7T6Cvp7eRMzg79IoVw88bl0dvZ3jNYPOzHMBeOA7E7kWExFNBXlxKN6J0Pon5FVnb2++65w==</Q><DP>1S0axgrmGk9kWCvWj1DTWmZUOdnxfPqLr7drytCnrprIwJ3JFwb4I50qmHXo3HQUOCOMVTLJePPsLzCPQUpJGw==</DP><DQ>lcjrVZ8K4FiAlSNN83Eyd4zmN5N55SbPxiAd9ICOqCmmcN37NbvB5voCabygda3g/VFyW6KyWJb5krYN1Xql0Q==</DQ><InverseQ>M2ggLAOBDR3WxHNI5pZeQIEk13oEQAUMLJCS7ahiFSEowleDhf7DvZVEUXwS9L2abvxunhnLkT8tyn/svy+V9w==</InverseQ><D>iejQ5TkhK0UHp307dfypW7VWz1pnM0S01QaUuBMRAFufxXzuBFE/t9aGdMdolRkIVnj/OQL4GJE+qcXFEv4oqjv/eO/pchmnI+/sXxESyt3wecO6YlHXQ3x/qWhesYnUzRSCoB9BMtHXv4xan2WIAZfYlWCGdnc4MObSC6LywlU=</D></RSAKeyValue>
PublicKey
XML 全选
<RSAKeyValue><Modulus>0nMMhiGAklb2URWmQsxABYYdrVqyiXfd9kfEVgBsdb39kAw6ivdvOs4ahYWbutcQRq37qRqlRHHAguDxcTWmxpWsFmd9c5DIwvOyXrmynSgGdEVzu5NrCfZlLI7WwvFSQxF/3smdgXQxXYcYwdItV8dXd3CxBaqWS92OzMLkIr0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
测试加密、解密
方案1:RSA公钥加密,RSA私钥解密
C# 全选
//方案1:RSA公钥加密
var data = RsaHelper.Encrypt("www.cscode.net|C/S框架网", publickey1);
Console.WriteLine(data);
//方案1:RSA私钥解密
var value = RsaHelper.Decrypt(data, privatekey1);
Console.WriteLine(value);
方案2:RSA私钥加密,RSA私钥解密
C# 全选
//方案2:RSA私钥加密
var data1 = RsaHelper.Encrypt("www.cscode.net|C/S框架网", privatekey1);
//方案2:RSA私钥解密
var value1 = RsaHelper.Decrypt(data, privatekey1);
数字签名、验签
前端使用公钥进行数字签名,后台使用私钥验签。
C# 全选
//数字签名、验签
var signData = RsaHelper.Sign("www.cscode.net|C/S框架网", publickey1);
var isEqual = RsaHelper.VerifySign("www.cscode.net|C/S框架网", signData, privatekey1);
Console.WriteLine(isEqual);
RsaHelper类
RSA算法加密解密(使用XML格式秘钥)
C# 全选
/// <summary>
/// RSA算法加密解密(使用XML格式秘钥)
/// </summary>
public static class RsaHelper
{
/// <summary>
/// 获取RSA 密钥
/// </summary>
/// <param name="privatekey">私钥</param>
/// <param name="publickey">公钥</param>
public static void GetXmlKey(out string privatekey, out string publickey)
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
privatekey = provider.ToXmlString(true);
publickey = provider.ToXmlString(false);
}
/// <summary>
/// RSA 使用公钥加密
/// </summary>
/// <param name="plainText">明文</param>
/// <param name="publicKey">公钥</param>
/// <returns></returns>
public static string Encrypt(string plainText, string publicKey)
{
byte[] buffer = Encoding.UTF8.GetBytes(plainText);
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(publicKey);
byte[] output = provider.Encrypt(buffer, false);
return Convert.ToBase64String(output);
}
/// <summary>
/// RSA 使用私钥解密
/// </summary>
/// <param name="encryptedText">明文</param>
/// <param name="privateKey">私钥</param>
/// <returns></returns>
public static string Decrypt(string encryptedText, string privateKey)
{
byte[] buffer = Convert.FromBase64String(encryptedText);
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKey);
byte[] output = provider.Decrypt(buffer, false);
return Encoding.UTF8.GetString(output);
}
/// <summary>
/// 使用公钥数字签名
/// </summary>
/// <param name="data">加密的数据</param>
/// <param name="publicKey">公钥</param>
/// <returns></returns>
public static string Sign(string data, string publicKey)
{
return Encrypt(data, publicKey);
}
/// <summary>
/// 使用私钥验签
/// </summary>
/// <param name="original">原文</param>
/// <param name="encryptedText">数字签名的数据</param>
/// <param name="privateKey">私钥</param>
/// <returns></returns>
public static bool VerifySign(string original, string encryptedText, string privateKey)
{
return Decrypt(encryptedText, privateKey).CompareTo(original) == 0;
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网