usingOrg.BouncyCastle.Math;usingOrg.BouncyCastle.Crypto.Parameters;usingOrg.BouncyCastle.Security;usingOrg.BouncyCastle.Asn1.X509;usingOrg.BouncyCastle.X509;usingOrg.BouncyCastle.Asn1.Pkcs;usingOrg.BouncyCastle.Pkcs;usingSystem;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Xml;namespaceUtilityHelper
{///
///RSA加密解密&数字签名///
public sealed classRSA
{#region RSA 加密解密
#region RSA 的密钥产生
///
///RSA产生密钥///
/// 私钥
/// 公钥
static public void RSAKey(out string xmlKeys, out stringxmlPublicKey)
{try{
System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();
xmlKeys= rsa.ToXmlString(true);
xmlPublicKey= rsa.ToXmlString(false);
}catch(Exception ex)
{throwex;
}
}#endregion
#region RSA加密函数
//##############################################################################//RSA 方式加密//KEY必须是XML的形式,返回的是字符串//该加密方式有长度限制的!//##############################################################################
///
///RSA的加密函数///
/// 公钥
/// 待加密的字符串
///
static public string RSAEncrypt(string xmlPublicKey, stringencryptString)
{try{byte[] PlainTextBArray;byte[] CypherTextBArray;stringResult;
System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray= (newUTF8Encoding()).GetBytes(encryptString);
CypherTextBArray= rsa.Encrypt(PlainTextBArray, false);
Result=Convert.ToBase64String(CypherTextBArray);returnResult;
}catch(Exception ex)
{throwex;
}
}///
///RSA的加密函数///
/// 公钥
/// 待加密的字节数组
///
static public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)
{try{byte[] CypherTextBArray;stringResult;
System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray= rsa.Encrypt(EncryptString, false);
Result=Convert.ToBase64String(CypherTextBArray);returnResult;
}catch(Exception ex)
{throwex;
}
}#endregion
#region RSA解密函数
///
///RSA的解密函数///
/// 私钥
/// 待解密的字符串
///
static public string RSADecrypt(string xmlPrivateKey, stringdecryptString)
{try{byte[] PlainTextBArray;byte[] DypherTextBArray;stringResult;
System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray=Convert.FromBase64String(decryptString);
DypherTextBArray= rsa.Decrypt(PlainTextBArray, false);
Result= (newUTF8Encoding()).GetString(DypherTextBArray);returnResult;
}catch(Exception ex)
{throwex;
}
}///
///RSA的解密函数///
/// 私钥
/// 待解密的字节数组
///
static public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
{try{byte[] DypherTextBArray;stringResult;
System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray= rsa.Decrypt(DecryptString, false);
Result= (newUnicodeEncoding()).GetString(DypherTextBArray);returnResult;
}catch(Exception ex)
{throwex;
}
}#endregion
static public string EncryptRSA(string publickey, stringplaintext)
{//RSA的加解密过程://有 rsa1 和 rsa2 两个RSA对象。//现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2//rsa2 获取得公钥之后,用来加密要发送的数据内容。//rsa1 获取