public static class RSAHelper
{
private static string privateKey;
private static string publicKey;
public static string GetPublicKey()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
publicKey = Convert.ToBase64String(rsa.ExportCspBlob(false));
privateKey = Convert.ToBase64String(rsa.ExportCspBlob(true));
return publicKey;
}
public static string Encrypt(string text)
{
byte[] byteEncrypt;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(Convert.FromBase64String(publicKey));
byte[] bytes = Encoding.UTF8.GetBytes(text);
byteEncrypt = rsa.Encrypt(bytes, false);
}
return Convert.ToBase64String(byteEncrypt);
}
public static string Decrypt(string text)
{
byte[] byteDecrypt;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(Convert.FromBase64String(privateKey));
byte[] bytes = Convert.FromBase64String(text);
byteDecrypt = rsa.Decrypt(bytes, false);
}
return Encoding.UTF8.GetString(byteDecrypt);
}
}