C# file Decryption - Bad Data

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2010-01-05T14:07:07Z Indexed on 2010/03/30 23:03 UTC
Read the original article Hit count: 588

Filed under:

Hi all,

I am in the process of rewriting an old application. The old app stored data in a scoreboard file that was encrypted with the following code:

private const String SSecretKey = @"?B?n?Mj?";
public DataTable GetScoreboardFromFile()
    {
        FileInfo f = new FileInfo(scoreBoardLocation);
        if (!f.Exists)
        {
            return setupNewScoreBoard();
        }

        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(SSecretKey);
        //Set initialization vector.
        DES.IV = ASCIIEncoding.ASCII.GetBytes(SSecretKey);

        //Create a file stream to read the encrypted file back.
        FileStream fsread = new FileStream(scoreBoardLocation, 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);

        DataTable dTable = new DataTable("scoreboard");
        dTable.ReadXml(new StreamReader(cryptostreamDecr));

        cryptostreamDecr.Close();
        fsread.Close();

        return dTable;
    }

This works fine. I have copied the code into my new app so that I can create a legacy loader and convert the data into the new format. The problem is I get a "Bad Data" error:

System.Security.Cryptography.CryptographicException was unhandled Message="Bad Data.\r\n" Source="mscorlib"

The error fires at this line:

dTable.ReadXml(new StreamReader(cryptostreamDecr));

The encrypted file was created today on the same machine with the old code. I guess that maybe the encryption / decryption process uses the application name / file or something and therefore means I can not open it.

Does anyone have an idea as to:

A) Be able explain why this isn't working? B) Offer a solution that would allow me to be able to open files that were created with the legacy application and be able to convert them please?

Thank you

© Stack Overflow or respective owner

Related posts about decryption