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 }