码迷,mamicode.com
首页 > 其他好文 > 详细

RSA公钥加密

时间:2016-06-04 19:37:06      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

 1         /// <summary>
 2         /// RSA公钥加密
 3         /// </summary>
 4         /// <param name="content">待加密文本</param>
 5         /// <param name="publickey">RSA公钥</param>
 6         /// <returns></returns>
 7         public static string RSAEncrypt(string content, string publickey)
 8         {
 9             string result = "";
10             try
11             {
12                 RSACryptoServiceProvider.UseMachineKeyStore = true;//防止出现 找不到指定文件 错误
13                 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
14                 RSAParameters RSAKeyInfo = new RSAParameters();
15                 byte[] bytearr = Convert.FromBase64String(publickey);
16                 RSAKeyInfo.Modulus = bytearr.ToList().GetRange(29, bytearr.Length - 34).ToArray();
17                 RSAKeyInfo.Exponent = Convert.FromBase64String("AQAB");
18                 RSA.ImportParameters(RSAKeyInfo);
19                 byte[] bytes = RSA.Encrypt(UTF8Encoding.UTF8.GetBytes(content), false);
20                 result = Convert.ToBase64String(bytes);
21             }
22             catch (Exception ex)
23             {
24                 log.Warn("RSA加密出错,加密文本:" + content + ",加密公钥:" + publickey + ",错误信息:" + ex.Message);
25             }
26             return result;
27         }

 

RSA公钥加密

标签:

原文地址:http://www.cnblogs.com/yixuehan/p/5559316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!