static byte[] desdecrypt(Mode mode, byte[] IV, byte[] key, byte[] msg)
{
using (var des = new DESCryptoServiceProvider())
{
des.IV = IV;
des.Key = key;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
using (var mstream = new MemoryStream())
{
CryptoStream cs = null;
if (mode == Mode.DECRYPT)
{
cs = new CryptoStream(mstream, des.CreateDecryptor(), CryptoStreamMode.Read);
}
if (cs == null)
return null;
cs.Read(msg, 0, msg.Length);
return mstream.ToArray();
}
}
return null;
}
private void button2_Click(object sender, EventArgs e)
{
string a = textBox4.Text;
string ab = textBox6.Text;
byte[] IV = Encoding.ASCII.GetBytes(ab);
string aa = textBox7.Text;
byte[] key = Encoding.ASCII.GetBytes(aa);
byte[] decrypted = desdecrypt(Mode.DECRYPT, IV, key, Encoding.ASCII.GetBytes(a));
textBox5.Text = Encoding.ASCII.GetString(decrypted);
}
此行中出现cs.Read(msg,0,msg.Length)错误(错误数据) 我的应用程序中发生了未处理的异常 我不知道有什么帮助,请我尝试了几乎所有的东西
答案 0 :(得分:1)
如果您在基础流中有数据并希望将其解密为数组,请使用CryptoStreamMode.Read
和Read
。
由于您有一个收集数据的空流,您应该使用CryptoStreamMode.Write
和Write
。
标准警告适用: