RAS實現加密
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到公鑰
myrsa.FromXmlString(publicKey);
//把你要加密的內容轉換成byte[]
byte[] PlainTextBArray = (new UnicodeEncoding()).GetBytes("這里是你要加密的內容");
//使用.NET中的Encrypt方法加密
byte[] CypherTextBArray = myrsa.Encrypt(PlainTextBArray, false);
//最后吧加密后的byte[]轉換成Base64String,這里就是加密后的內容了
Result = Convert.ToBase64String(CypherTextBArray)
RAS實現解密
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
//得到私鑰
myrsa.FromXmlString(xmlPrivateKey);
//把原來加密后的String轉換成byte[]
byte[] PlainTextBArray = Convert.FromBase64String("剛才加密后的string");
//使用.NET中的Decrypt方法解密
byte[] DypherTextBArray = myrsa.Decrypt(PlainTextBArray, false);
//轉換解密后的byte[],這就得到了我們原來的加密前的內容了
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");
//公鑰驗證
RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
oRSA4.FromXmlString(publickey);
bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", AOutput);
新聞熱點
疑難解答