雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#中RSA加密與解密的實(shí)例詳解

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊:

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;
    }
  }

測(cè)試:

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加密與解密  --  使用同一個(gè)密鑰容器進(jìn)行加密與解密

 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);
    }
  }

測(cè)試 :

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);

上一篇:百度人臉識(shí)別之人臉識(shí)別FaceIdentify(簽到考勤)

欄    目:C#教程

下一篇:C#讀取XML的CDATA節(jié)點(diǎn)內(nèi)容實(shí)例詳解

本文標(biāo)題:C#中RSA加密與解密的實(shí)例詳解

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4669.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有