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

RSA加密(跨平台通用的)

时间:2014-06-07 06:50:15      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

bubuko.com,布布扣
 1      /// <summary>
 2         /// RSA加密
 3         /// </summary>
 4         /// <param name="strPublickey"></param>
 5         /// <param name="content"></param>
 6         /// <returns></returns>
 7         public string RsaEncrypt(string strPublickey, string content)
 8         {
 9             var rsa = new RSACryptoServiceProvider();
10             var param = new RSAParameters();
11             byte[] bdata = GetBytes("65537");
12             param.Exponent = bdata;
13             param.Modulus = GetBytes(strPublickey);
14             rsa.ImportParameters(param);
15 
16             byte[] cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content),false);
17 
18             return BitConverter.ToString(cipherbytes).Replace("-", string.Empty);
19         }
20 
21         public byte[] GetBytes(String num)
22         {
23             BigInteger n = new BigInteger(num, 10);
24             String s = n.ToString(2);
25             if (s.Length % 8 > 0)
26             {
27                 s = new String(0, 8 - s.Length % 8) + s;
28             }
29             byte[] data = new byte[s.Length / 8];
30             String ocetstr;
31             for (int i = 0; i < data.Length; i++)
32             {
33                 ocetstr = s.Substring(8 * i, 8);
34                 data[i] = Convert.ToByte(ocetstr, 2);
35             }
36             return data;
37         }
       调用方式
       var result = new BigInteger(pubkey, 16);
           String sp = RsaEncrypt(string.Format("{0}", result), message);
       BigInteger下载地址
       http://www.codeproject.com/Articles/2728/C-BigInteger-Class
bubuko.com,布布扣

 

RSA加密(跨平台通用的),布布扣,bubuko.com

RSA加密(跨平台通用的)

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/PROLION/p/3770642.html

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