RSA公钥加密
Posted 巧言新色
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RSA公钥加密相关的知识,希望对你有一定的参考价值。
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公钥加密的主要内容,如果未能解决你的问题,请参考以下文章
php/js/linux: js加密(rsa公钥加密) php解密(rsa私钥解密)