欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#中RSA加密与解密的实例详解

时间:2021-07-18 08:24:32|栏目:.NET代码|点击:

1.  RSA加密与解密  --  使用公钥加密、私钥解密

public class RSATool
 {
  public string Encrypt(string strText, string strPublicKey)
  {
   RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
   rsa.FromXmlString(strPublicKey);
   byte[] byteText = Encoding.UTF8.GetBytes(strText);
   byte[] byteEntry = rsa.Encrypt(byteText, false);
   return Convert.ToBase64String(byteEntry);
  }
  public string Decrypt(string strEntryText,string strPrivateKey)
  {
   RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
   rsa.FromXmlString(strPrivateKey);
   byte[] byteEntry = Convert.FromBase64String(strEntryText);
   byte[] byteText = rsa.Decrypt(byteEntry, false);
   return Encoding.UTF8.GetString(byteText);
  }
  public Dictionary<string,string> GetKey()
  {
   Dictionary<string, string> dictKey = new Dictionary<string, string>();
   RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
   dictKey.Add("PublicKey", rsa.ToXmlString(false));
   dictKey.Add("PrivateKey", rsa.ToXmlString(true));
   return dictKey;
  }
 }

测试:

RSATool myRSA = new RSATool();
   Dictionary<string, string> dictK = new Dictionary<string, string>();
   dictK = myRSA.GetKey();
   string strText = "123456";
   Console.WriteLine("要加密的字符串是:{0}", strText);
   string str1 = myRSA.Encrypt("123456", dictK["PublicKey"]);
   Console.WriteLine("加密后的字符串:{0}", str1);
   string str2 = myRSA.Decrypt(str1, dictK["PrivateKey"]);
   Console.WriteLine("解密后的字符串:{0}", str2);

2.  RSA加密与解密  --  使用同一个密钥容器进行加密与解密

 public class RSAToolX
 {
  public string Encrypt(string strText)
  {
   CspParameters CSApars = new CspParameters();
   CSApars.KeyContainerName = "Test001";
   RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSApars);
   byte[] byteText = Encoding.UTF8.GetBytes(strText);
   byte[] byteEntry = rsa.Encrypt(byteText, false);
   return Convert.ToBase64String(byteEntry);
  }
  public string Decrypt(string strEntryText)
  {
   CspParameters CSApars = new CspParameters();
   CSApars.KeyContainerName = "Test001";
   RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSApars);
   byte[] byteEntry = Convert.FromBase64String(strEntryText);
   byte[] byteText = rsa.Decrypt(byteEntry, false);
   return Encoding.UTF8.GetString(byteText);
  }
 }

测试 :

RSAToolX myRSA = new RSAToolX();
   string strText = "123456";
   Console.WriteLine("要加密的字符串是:{0}", strText);
   string str1 = myRSA.Encrypt("123456");
   Console.WriteLine("加密后的字符串:{0}", str1);
   string str2 = myRSA.Decrypt(str1);
   Console.WriteLine("解密后的字符串:{0}", str2);

总结

以上所述是小编给大家介绍的C#中RSA加密与解密的实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

上一篇:WinForm下 TextBox只允许输入数字的小例子

栏    目:.NET代码

下一篇:使用C#实现在屏幕上画图效果的代码实例

本文标题:C#中RSA加密与解密的实例详解

本文地址:http://www.codeinn.net/misctech/158578.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有