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);
//把原來(lái)加密后的String轉(zhuǎn)換成byte[]
byte[] PlainTextBArray = Convert.FromBase64String("剛才加密后的string");
//使用.NET中的Decrypt方法解密
byte[] DypherTextBArray = myrsa.Decrypt(PlainTextBArray, false);
//轉(zhuǎn)換解密后的byte[],這就得到了我們?cè)瓉?lái)的加密前的內(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);
新聞熱點(diǎn)
疑難解答
圖片精選