-1

I have an XML file with information about my stringConnection parameters which decrypted with this method:

public void DecryptFile(string sInputFileName, string sOutputFileName, string sKey)
{
    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    FileStream fsread = new FileStream(sInputFileName, FileMode.Open, FileAccess.Read);
    ICryptoTransform desdecrypt = DES.CreateDecryptor();
    CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);

    //Print result
    StreamWriter fsDecrypted = new StreamWriter(sOutputFileName);
    //In this point ReadToEnd return the final xml result decrypted
    fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
    fsDecrypted.Flush();
    fsDecrypted.Close();
    fsread.Close();
    fsread.Dispose();
}

The result would be this:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <TU>
    <BD>DataBaseName</BD>
    <SR>ServerName</SR>
    <USR>UserDB</USR>
    <CONT>Pass</CONT>
    <EMP>codCompany</EMP>
    <EMPDES>companyName</EMPDES>
    <SUC>codLocal</SUC>
  </TU>
</NewDataSet>

But ReadToEnd doesn't return the last label of my XML file, like this:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <TU>
    <BD>DataBaseName</BD>
    <SR>ServerName</SR>
    <USR>UserDB</USR>
    <CONT>Pass</CONT>
    <EMP>codCompany</EMP>
    <EMPDES>companyName</EMPDES>
    <SUC>codLocal</SUC>
  </TU>
</NewDataSet

What's wrong with my method?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • 1
    You may want to provide the `EncryptFile` method as well. I would also make sure you start adding `using` statements to your code to guarantee proper cleanup of `IDisposable` types. – ChaosPandion Mar 19 '18 at 03:16

1 Answers1

1

The main issue is that you are not really reading the CryptoStream to the end, because of the way it blocks data (as pointed out in this answer). Here is an approach adapted from this answer which uses a buffer to read 1024 bytes at a time.

public void DecryptFile(string sInputFileName, string sOutputFileName, string sKey)
{
    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

    ICryptoTransform desdecrypt = DES.CreateDecryptor();
    using (FileStream fsread = new FileStream(sInputFileName, FileMode.Open, FileAccess.Read))
    using (StreamWriter fsDecrypted = new StreamWriter(sOutputFileName))
    using (CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read))
    {
        //Print result
        var buffer = new byte[1024];
        var read = cryptostreamDecr.Read(buffer, 0, buffer.Length);
        while (read > 0)
        {
            fsDecrypted.Write(buffer, 0, read);
            read = cryptostreamDecr.Read(buffer, 0, buffer.Length);
        }
    }
}

You are also missing using statements, which ensure your streams are disposed correctly whether or not your program has an exception.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • `cryptostreamDecr.CopyTo(fsDecrypted)` should work just as well, and be a bit cleaner from a code review perspective. – bartonjs Mar 19 '18 at 15:38