rsa加密算法使用示例分享
產(chǎn)生私鑰和公鑰
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到私鑰主要保存了RSAParameters中的8各參數(shù)
privateKey = myrsa.ToXmlString(true);
//得到公鑰保存了RSAParameters中2個參數(shù)
publicKey = myrsa.ToXmlString(false);
RAS實(shí)現(xiàn)加密
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到公鑰
myrsa.FromXmlString(publicKey);
//把你要加密的內(nèi)容轉(zhuǎn)換成byte[]
byte[] PlainTextBArray = (new UnicodeEncoding()).GetBytes("這里是你要加密的內(nèi)容");
//使用.NET中的Encrypt方法加密
byte[] CypherTextBArray = myrsa.Encrypt(PlainTextBArray, false);
//最后吧加密后的byte[]轉(zhuǎn)換成Base64String,這里就是加密后的內(nèi)容了
Result = Convert.ToBase64String(CypherTextBArray)
RAS實(shí)現(xiàn)解密
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到私鑰
myrsa.FromXmlString(xmlPrivateKey);
//把原來加密后的String轉(zhuǎn)換成byte[]
byte[] PlainTextBArray = Convert.FromBase64String("剛才加密后的string");
//使用.NET中的Decrypt方法解密
byte[] DypherTextBArray = myrsa.Decrypt(PlainTextBArray, false);
//轉(zhuǎn)換解密后的byte[],這就得到了我們原來的加密前的內(nèi)容了
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
byte[] messagebytes = Encoding.UTF8.GetBytes("luo羅");
RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
string privatekey = oRSA.ToXmlString(true);
string publickey = oRSA.ToXmlString(false);
//私鑰簽名
RSACryptoServiceProvider oRSA3 = new RSACryptoServiceProvider();
oRSA3.FromXmlString(privatekey);
byte[] AOutput = oRSA3.SignData(messagebytes, "SHA1");
//公鑰驗(yàn)證
RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
oRSA4.FromXmlString(publickey);
bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", AOutput);
相關(guān)文章
c# 通過經(jīng)緯度查詢 具體的地址和區(qū)域名稱
最近項(xiàng)目需要通過經(jīng)緯度查詢 具體的地址和區(qū)域名稱,通過查詢網(wǎng)絡(luò)資源,發(fā)現(xiàn)提供的大多是得到具體的地址而對區(qū)域或城市名稱的獲取就不是很好把握;在這里自己搞了個,需要的朋友可以參考下2012-11-11
C#簡易人機(jī)對抗“石頭剪刀布”游戲的實(shí)現(xiàn)
本文主要介紹了C#簡易人機(jī)對抗“石頭剪刀布”游戲的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C# Lambda表達(dá)式及Lambda表達(dá)式樹的創(chuàng)建過程
這篇文章主要介紹了C# Lambda表達(dá)式及Lambda表達(dá)式樹的創(chuàng)建過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
C#中Monitor對象與Lock關(guān)鍵字的區(qū)別分析
這篇文章主要介紹了C#中Monitor對象與Lock關(guān)鍵字的區(qū)別,需要的朋友可以參考下2013-06-06

