using System;
using System.Text;
using System.Security.Cryptography;
namespace DotNet.Utilities
{
/// <summary>
/// RSA加密解密及RSA签名和验证
/// </summary>
public class RSACryption
{
public RSACryption()
{
}
#region RSA 加密解密
#region RSA 的密钥产生
/// <summary>
/// RSA 的密钥产生 产生私钥 和公钥
/// </summary>
/// <param name="xmlKeys"></param>
/// <param name="xmlPublicKey"></param>
public void RSAKey(out string xmlKeys,out string xmlPublicKey)
{
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
xmlKeys=rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
#endregion
#region RSA的加密函数
//##############################################################################
//RSA 方式加密
//说明KEY必须是XML的行式,返回的是字符串
//在有一点需要说明!!该加密方式有 长度 限制的!!
//##############################################################################
//RSA的加密函数 string
public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
//RSA的加密函数 byte[]
public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
{
byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(EncryptString, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
#endregion
#region RSA的解密函数
//RSA的解密函数 string
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System
C# RSA加密解密及RSA签名和验证
最新推荐文章于 2024-10-07 14:27:44 发布