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 = 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