1 /// <summary> 2 /// RSA 加密 3 /// </summary> 4 public static string EncryptByRSA(this string source) 5 { 6 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 7 rsa.FromXmlString(PublicRSAKey); 8 var cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(source), false); 9 return Convert.ToBase64String(cipherbytes); 10 } 11 12 /// <summary> 13 /// RSA解密 14 /// </summary> 15 public static string DecryptByRSA(this string source) 16 { 17 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 18 rsa.FromXmlString(PrivateRSAKey); 19 var cipherbytes = rsa.Decrypt(Convert.FromBase64String(source), false); 20 return Encoding.UTF8.GetString(cipherbytes); 21 }
【原文出处】 http://www.51aras.com/?id=28