C# RSA加密解密
应用场景:开放接口给外部调用,为了防止数据被恶意篡改,或者访问的合法性 ,用RSA对数据进行加密。 RSA会生成公钥和私钥,公钥用来加密,私钥用来解密。
RSA加密算法是一种非对称加密算法。在公钥加密标准和电子商业中RSA被广泛使用。RSA是1977年由罗纳德李维斯特(Ron Rivest)、阿迪萨莫尔(Adi Shamir)和伦纳德阿德曼(Leonard Adleman)一起提出的。当时他们三人都在麻省理工学院工作。RSA就是他们三人姓氏开头字母拼在一起组成的。
RSA 具体的逻辑就不写了,网上一大堆,看着挺复杂,我了解了下原理,没深入理解,这里就不粘了。
下面粘下具体的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static class RSACryption { #region RSA 加密解密 #region RSA 的密钥产生 /// <summary> /// RSA 的密钥产生 产生私钥 和公钥 /// </summary> /// <param name="xmlPrivateKey"></param> /// <param name="xmlPublicKey"></param> public static void RSAKey() { //System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); //xmlPrivateKey = rsa.ToXmlString(true); //xmlPublicKey = rsa.ToXmlString(false); string path = System.AppDomain.CurrentDomain.BaseDirectory; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); using (StreamWriter writer = new StreamWriter(path + @"\PrivateKey.xml")) //这个文件要保密... { writer.WriteLine(rsa.ToXmlString(true)); } using (StreamWriter writer = new StreamWriter(path + @"\PublicKey.xml")) { writer.WriteLine(rsa.ToXmlString(false)); } } #endregion #region RSA的加密函数 //############################################################################## //RSA 方式加密 //说明KEY必须是XML的行式,返回的是字符串 //在有一点需要说明!!该加密方式有 长度 限制的!! //############################################################################## //RSA的加密函数 string public static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString); CypherTextBArray = rsa.Encrypt(PlainTextBArray, false); Result = Convert.ToBase64String(CypherTextBArray); return Result; } //RSA的加密函数 byte[] public static string RSAEncrypt(string xmlPublicKey, byte[] EncryptString) { byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); CypherTextBArray = rsa.Encrypt(EncryptString, false); Result = Convert.ToBase64String(CypherTextBArray); return Result; } #endregion #region RSA的解密函数 //RSA的解密函数 string public static string RSADecrypt(string xmlPrivateKey, string m_strDecryptString) { byte[] PlainTextBArray; byte[] DypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); PlainTextBArray = Convert.FromBase64String(m_strDecryptString); DypherTextBArray = rsa.Decrypt(PlainTextBArray, false); Result = (new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } //RSA的解密函数 byte public static string RSADecrypt(string xmlPrivateKey, byte[] DecryptString) { byte[] DypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); DypherTextBArray = rsa.Decrypt(DecryptString, false); Result = (new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } #endregion #endregion }
一、RSA加密
1、加密方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 加密 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost] public IHttpActionResult RSAEncrypt(RSARequest model) { string encryptionStr = RSACryption.RSAEncrypt(model.XmlPublicKey, model.StrEncryptString); return Ok(new { code = ResultCode.Success, encryptionStr = encryptionStr, message = "加密成功!" }); }
2、传递参数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class RSARequest { /// <summary> /// 公钥 /// </summary> public string XmlPublicKey { get; set; } /// <summary> /// 加密字符串 /// </summary> public string StrEncryptString { get; set; } }
3、调用结果
二、RSA解密
1、解密
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
XmlDocument doc = new XmlDocument(); string path = System.AppDomain.CurrentDomain.BaseDirectory; doc.Load(path + @"\PrivateKey.xml"); string token = RSACryption.RSADecrypt(doc.InnerXml, model.AccessToken);
PS:需要注意的是无论是公钥还是私钥,用来加密和解密传参数的时候,需要传递的是生成的公钥和私钥的全部内容。读取全部的xml看成是一个字符串,不要试图去提取里面的参数传递,那样是不对的。