2013-03-29 42 views
-1

我有一个“坏数据异常”试图解密C#。任何帮助,使用DES将great.here一个文件时,是我的代码:坏数据异常

namespace encrypte 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) 
     { 
      FileStream fsInput = new FileStream(sInputFilename,FileMode.Open, FileAccess.Read); 
      FileStream fsEncrypted = new FileStream(sOutputFilename,FileMode.Create,FileAccess.Write); 
      DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 
      DES.Mode = CipherMode.CFB; 
      DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 
      DES.Padding = PaddingMode.ISO10126; 
      ICryptoTransform desencrypt = DES.CreateEncryptor(); 
      CryptoStream cryptostream = new CryptoStream(fsEncrypted,desencrypt,CryptoStreamMode.Write); 
      byte[] bytearrayinput = new byte[fsInput.Length - 1]; 
      fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); 
      cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); 
      cryptostream.FlushFinalBlock(); 
      fsInput.Close(); 
      fsEncrypted.Close(); 
     } 
     static void DecryptFile(string sInputFilename,string sOutputFilename,string sKey) 
     { 
      DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 
      DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 
      DES.Mode = CipherMode.CFB; 
      DES.Padding = PaddingMode.ISO10126; 
      FileStream fsread = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read); 
      ICryptoTransform desdecrypt = DES.CreateDecryptor(); 
      CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt,CryptoStreamMode.Read); 
      StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); 
      fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); 
      fsDecrypted.Flush(); 
      fsDecrypted.Close(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      EncryptFile(@"E:\a.wmv", @"E:\b.wmv", "abcdefgh"); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      DecryptFile(@"E:\b.wmv", @"E:\c.wmv", "abcdefgh"); 
     } 
    } 
} 

我得到了一个“坏数据“异常: fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());

+1

你在这里面临什么问题?在发生此异常的地方提供详细的异常消息和行号。 – Yasser

+0

您正在使用TextReader/TextWriter进行解密。这是错误的。 – leppie

+0

当我添加cryptostream.FlushFinalBlock();在encrypte函数中,我得到了一个大于加密大小的解密文件(b.wmv为22.6mb,c.wmv为39.3 mb)。 –

回答

2
static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey) 
{ 
    DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 
    DES.Mode = CipherMode.CFB; 
    DES.Padding = PaddingMode.ISO10126; 
    FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); 
    ICryptoTransform desdecrypt = DES.CreateDecryptor(); 
    CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); 
    FileStream fsDecrypted = new FileStream(sOutputFilename,FileMode.Create,FileAccess.Write); 
    cryptostreamDecr.CopyTo(fsDecrypted); 
    fsDecrypted.Flush(); 
    fsDecrypted.Close(); 
} 
+0

+1:为了做我懒得做的事情; p – leppie

+0

@ DarkSquirrel42:谢谢你的帮助,现在问题解决了。 –