.net 非对称加密
后台
public class RSAHelper
{
/// <summary>
/// RSA加密
/// </summary>
/// <param name="algorithmName"></param>
/// <returns></returns>
public static String Encrypt(String algorithmName)
{
int rsa = 1;
CspParameters cspParms = new CspParameters(rsa);
cspParms.Flags = CspProviderFlags.UseMachineKeyStore;
cspParms.KeyContainerName = "ASSCSSSS";
RSACryptoServiceProvider algorithm = new RSACryptoServiceProvider(cspParms);
byte[] sourceBytes = new UnicodeEncoding().GetBytes(algorithmName);
byte[] rasCipherText = algorithm.Encrypt(sourceBytes, true);
return Convert.ToBase64String(rasCipherText);
}
/// <summary>
/// RSA开始解密
/// </summary>
/// <param name="encryptedText"></param>
/// <returns></returns>
public static String Decrypt(String encryptedText)
{
var rsa = 1;
// decrypt the data.
byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
var cspParms = new CspParameters(rsa);
cspParms.Flags = CspProviderFlags.UseMachineKeyStore;
cspParms.KeyContainerName = "ASSCSSSS";
RSACryptoServiceProvider algorithm = new RSACryptoServiceProvider(cspParms);
byte[] unencrypted = algorithm.Decrypt(encryptedBuffer, true);
String Decrytoed = new UnicodeEncoding().GetString(unencrypted);
return Decrytoed;
}
}
前台
String text = "欢迎光临!www.ritztours.com && 成功了";
String Encryptoed = RSAHelper.Encrypt(text);
String Decryptoed = RSAHelper.Decrypt(Encryptoed);