.Net中使用RSA加密解密方法

 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

  

posted @ 2019-07-30 13:08  无敌师爷IT技术Blog  阅读(1929)  评论(0编辑  收藏  举报