title: C# DES File Decryption Breaking Non-Text Files
body: <p>I have these two methods which are pretty much copy+pastes from <a href="http://support.microsoft.com/kb/307010" rel="nofollow">http://support.microsoft.com/kb/307010</a>.</p>
  
  <p>When I decrypt the files, if they are any type of text file such as .txt, .xml, .html, etc. I can open them up and everything is fine. Any type of file not just text, such as .exe, .jpg, .pdf, etc. all break when decrypted. Is there anything I am doing wrong? Are these methods using binary to encrypt/decrypt the files? If not is there a way I can make it binary?</p>
  
  <p>Any help is greatly appreciated!</p>
  
  <pre><code>public 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.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
          DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
          ICryptoTransform desencrypt = DES.CreateEncryptor();
          CryptoStream cryptostream = new CryptoStream(fsEncrypted,
             desencrypt,
             CryptoStreamMode.Write);
  
          byte[] bytearrayinput = new byte[fsInput.Length];
          fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
          cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
          cryptostream.Close();
          fsInput.Close();
          fsEncrypted.Close();
      }
  
      public static void DecryptFile(string sInputFilename,
         string sOutputFilename,
         string sKey)
      {
          DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
          //A 64 bit key and IV is required for this provider.
          //Set secret key For DES algorithm.
          DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
          //Set initialization vector.
          DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  
          //Create a file stream to read the encrypted file back.
          FileStream fsread = new FileStream(sInputFilename,
             FileMode.Open,
             FileAccess.Read);
          //Create a DES decryptor from the DES instance.
          ICryptoTransform desdecrypt = DES.CreateDecryptor();
          //Create crypto stream set to read and do a 
          //DES decryption transform on incoming bytes.
          CryptoStream cryptostreamDecr = new CryptoStream(fsread,
             desdecrypt,
             CryptoStreamMode.Read);
          //Print the contents of the decrypted file.
          StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
          fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
          fsDecrypted.Flush();
          fsDecrypted.Close();
          fsread.Close();
          cryptostreamDecr.Close();
      }
  </code></pre>