首页 > 代码库 > RSA加密(跨平台通用的)
RSA加密(跨平台通用的)
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 = http://www.mamicode.com/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 = http://www.mamicode.com/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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。