在ASP.NET中使用RSA算法进行加密,解密的时候出错,请问是什么情况?代码和错误如下:
string seckey;
string pubkey;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
seckey = rsa.ToXmlString(true);
pubkey = rsa.ToXmlString(false); byte[] source = (new UnicodeEncoding()).GetBytes(txtInput.Text);
byte[] bytes_Cypher_Text = rsa.Encrypt(source, false);
string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
txtOutput1.Text = str_Cypher_Text; RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
rsa1.FromXmlString(pubkey); //byte[] DataToDecrypt = Convert.FromBase64String(txtOutput1.Text);
byte[] bytes_Plain_Text = rsa1.Decrypt(bytes_Cypher_Text, false);
UnicodeEncoding ByteConverter = new UnicodeEncoding();
txtOutput2.Text = ByteConverter.GetString(bytes_Plain_Text);
异常详细信息: System.Security.Cryptography.CryptographicException: 不正确的项。
源错误:
行 90:
行 91: //byte[] DataToDecrypt = Convert.FromBase64String(txtOutput1.Text);
行 92: byte[] bytes_Plain_Text = rsa1.Decrypt(bytes_Cypher_Text, false);
行 93: UnicodeEncoding ByteConverter = new UnicodeEncoding();
行 94: txtOutput2.Text = ByteConverter.GetString(bytes_Plain_Text);
string seckey;
string pubkey;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
seckey = rsa.ToXmlString(true);
pubkey = rsa.ToXmlString(false); byte[] source = (new UnicodeEncoding()).GetBytes(txtInput.Text);
byte[] bytes_Cypher_Text = rsa.Encrypt(source, false);
string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
txtOutput1.Text = str_Cypher_Text; RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
rsa1.FromXmlString(pubkey); //byte[] DataToDecrypt = Convert.FromBase64String(txtOutput1.Text);
byte[] bytes_Plain_Text = rsa1.Decrypt(bytes_Cypher_Text, false);
UnicodeEncoding ByteConverter = new UnicodeEncoding();
txtOutput2.Text = ByteConverter.GetString(bytes_Plain_Text);
异常详细信息: System.Security.Cryptography.CryptographicException: 不正确的项。
源错误:
行 90:
行 91: //byte[] DataToDecrypt = Convert.FromBase64String(txtOutput1.Text);
行 92: byte[] bytes_Plain_Text = rsa1.Decrypt(bytes_Cypher_Text, false);
行 93: UnicodeEncoding ByteConverter = new UnicodeEncoding();
行 94: txtOutput2.Text = ByteConverter.GetString(bytes_Plain_Text);
using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace RX.SynchHistoryData.Utility { /// <summary> /// 根据对称算法对数据进行加密和解密 /// </summary> public class CryptClass { private string txtKey = "lycompsofts="; private string txtIV = "lycompsofts="; private SymmetricAlgorithm mCSP; public CryptClass() { } private void keyIv() { mCSP = new DESCryptoServiceProvider(); mCSP.Key = Convert.FromBase64String(txtKey); mCSP.IV = Convert.FromBase64String(txtIV); } /// <summary> /// 加密 /// </summary> /// <param name="str">待加密数据 </param> /// <param name="sKey">密钥 </param> /// <param name="sIV">初始化向量 </param> /// <returns> </returns> public string EncryptString(string str, string sKey, string sIV) { txtKey = sKey; txtIV = sIV; keyIv(); ICryptoTransform ct; MemoryStream ms; CryptoStream cs; byte[] byt; ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV); byt = Encoding.UTF8.GetBytes(str); ms = new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Convert.ToBase64String(ms.ToArray()); } /// <summary> /// 解密 /// </summary> /// <param name="str">已加密数据 </param> /// <param name="sKey">密钥 </param> /// <param name="sIV">初始化向量 </param> /// <returns> </returns> public string DecryptString(string str, string sKey, string sIV) { txtKey = sKey; txtIV = sIV; keyIv(); ICryptoTransform ct; MemoryStream ms; CryptoStream cs; byte[] byt; ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV); byt = Convert.FromBase64String(str); ms = new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Encoding.UTF8.GetString(ms.ToArray()); } } }
本篇文章来自<A href='http://www.soidc.net'>IDC专家网</a> 原文链接:http://www.soidc.net/discuss/5/040101/00/555524_1.html