RSA加密
需要引入的命名空间
using System.Security;
using System.Security.Cryptography;
//加密字符串
public static CspParameters param;
public static string RsaEncrypt(string str)
{
param = new CspParameters();
//密匙容器的名称,保持加密解密一致才能解密成功
param.KeyContainerName = "lph";
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
//将要加密的字符串转换为字节数组
byte[] plaindata = Encoding.Default.GetBytes(str);
byte[] encry = rsa.Encrypt(plaindata, false);
string data = Convert.ToBase64String(encry);
return data;
}
}
//解密字符串
public static string RsaDesEncrypt(string str)
{
param = new CspParameters();
param.KeyContainerName = "lph";
using (RSACryptoServiceProvider rsa= new RSACryptoServiceProvider(param))
{
byte[] encryptdata = Convert.FromBase64String(str);
byte[] decryptdata = rsa.Decrypt(encryptdata,false);
string data = Encoding.Default.GetString(decryptdata);
return data;
}
}