1. RSA加密與解密 -- 使用公鑰加密、私鑰解密
創(chuàng)新互聯(lián)是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站備案、服務(wù)器租用、申請(qǐng)域名、軟件開(kāi)發(fā)、小程序定制開(kāi)發(fā)等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營(yíng)推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開(kāi)一個(gè)面向全國(guó)乃至全球的業(yè)務(wù)窗口:建站服務(wù)電話:028-86922220
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);
總結(jié)
以上所述是小編給大家介紹的C#中RSA加密與解密的實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
標(biāo)題名稱(chēng):C#中RSA加密與解密的實(shí)例詳解
文章轉(zhuǎn)載:http://jinyejixie.com/article8/psgdop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、網(wǎng)站排名、品牌網(wǎng)站建設(shè)、微信公眾號(hào)、標(biāo)簽優(yōu)化、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)