C#“坏数据”解密加密文件时出现异常 [英] C# "Bad Data" exception when decrypting encrypted file
问题描述
public static void EncryptFile(string strInputFileName,string strOutputFileName,string strKey )
{
FileStream fsIn = new FileStream(strInputFileName,FileMode.Open,FileAccess.Read);
FileStream fsOut = new FileStream(strOutputFileName,FileMode.Create,FileAccess.Write);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
ICryptoTransform desencrypt = des.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsOut,desencrypt,CryptoStreamMode.Write);
byte [] byteArrayInput = new byte [fsIn.Length - 1];
fsIn.Read(byteArrayInput,0,byteArrayInput.Length);
cryptostream.Write(byteArrayInput,0,byteArrayInput.Length);
fsIn.Close();
fsOut.Close();
}
方法成功完全加密文件。这是我的解密方法:
public static void DecryptFile(string strInputFileName,string strOutputFileName,string strKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
byte [] te =新字节[1024];
FileStream fsRead = new FileStream(strInputFileName,FileMode.Open,FileAccess.Read);
ICryptoTransform desdecrypt = des.CreateDecryptor();
CryptoStream cryptostream = new CryptoStream(fsRead,desdecrypt,CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(strOutputFileName);
fsDecrypted.Write(new StreamReader(cryptostream).ReadToEnd()); //这是坏数据发生的地方。
fsDecrypted.Flush();
fsDecrypted.Close();
fsRead.Close();
}
当我检查cryptostream对象时,它表示已经抛出异常,Stream不支持搜索。
任何帮助将不胜感激!
这里:
cryptostream.Write(byteArrayInput,0,byteArrayInput.Length);
fsIn.Close();
fsOut.Close();
直接关闭 fsOut
关闭 cryptostream
。这意味着加密流没有机会刷新任何最终的块等。
另外:
- 使用语句,而不是手动调用Close或Dispose
- 您正在调用
读取
一次,假设它将读取所有数据 - 您不会检查返回值。 (你也在删除输入文件的最后一个字节,为什么?)一般来说,你应该循环,读入缓冲区,然后写出你读取的很多字节,直到Read方法返回0 。如果您使用的是.NET 4,Stream.CopyTo
是您的朋友。
Hey I'm very new to encryption and decryption, or even the c# language to be honest. Basically, I have a tcp chat server that "saves" logs and encrypts the text file. This is how I encrypt (based from the MSDN sample):
public static void EncryptFile(string strInputFileName, string strOutputFileName, string strKey)
{
FileStream fsIn = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read);
FileStream fsOut = new FileStream(strOutputFileName, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
ICryptoTransform desencrypt = des.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsOut, desencrypt, CryptoStreamMode.Write);
byte[] byteArrayInput = new byte[fsIn.Length - 1];
fsIn.Read(byteArrayInput, 0, byteArrayInput.Length);
cryptostream.Write(byteArrayInput, 0, byteArrayInput.Length);
fsIn.Close();
fsOut.Close();
}
The method success fully encrypts files. This is my decrypt method:
public static void DecryptFile(string strInputFileName, string strOutputFileName, string strKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
byte[] te = new byte[1024];
FileStream fsRead = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read);
ICryptoTransform desdecrypt = des.CreateDecryptor();
CryptoStream cryptostream = new CryptoStream(fsRead, desdecrypt, CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(strOutputFileName);
fsDecrypted.Write(new StreamReader(cryptostream).ReadToEnd());//This is where the "Bad Data" occurs.
fsDecrypted.Flush();
fsDecrypted.Close();
fsRead.Close();
}
And when I inspect the cryptostream object, it says that it has thrown an exception, "Stream does not support seeking".
Any help would be greatly appreciated!
Here:
cryptostream.Write(byteArrayInput, 0, byteArrayInput.Length);
fsIn.Close();
fsOut.Close();
You're closing fsOut
directly, without closing cryptostream
. That means the crypto stream doesn't get the chance to flush any final blocks etc.
Additionally:
- Use
using
statements instead of manually calling Close or Dispose - You're currently calling
Read
once, and assuming it will read all the data - you're not checking the return value. (You're also removing the last byte of the input file for some reason... why?) In general, you should loop round, reading into a buffer and then writing out however many bytes you read, until the Read method returns 0. If you're using .NET 4,Stream.CopyTo
is your friend.
这篇关于C#“坏数据”解密加密文件时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!