0

So im making a encryption/decryption windows forms project for fun, but my decryption app shows me a error: An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll

Additional information: Bad Data.

I can't find any fixes in the internet and in not that good in c#, so maybe you can help me.

Encryption:

static void EncryptFile(string sInputFile,
        string sOutputFile,
        string sKey)
    {
        FileStream fsInput = new FileStream(sInputFile, FileMode.Open, FileAccess.Read);
        FileStream fsEncrypted = new FileStream(sOutputFile, 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 - 1];
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
        cryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length);

    }

Decryption:

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);
        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();
    }

How i use them:

EncryptFile(fileBox.Text, fileOutFolder+"/encrypted.txt", sSecretKey);
DecryptFile(fileBox.Text, saveFileDialog1.FileName, keyBox.Text)
  • cryptoStream missing Flush() and Close() ? – Cologler May 09 '16 at 05:52
  • 1
    better way is use `using () {}`. – Cologler May 09 '16 at 05:53
  • The error is when i use decryption. But ill check. – Marks Kristiāns Ābele May 09 '16 at 06:23
  • 3
    if cryptoStream missing Flush(), then your data was not write to disk. you lose your encrypted data. when you decrypt data, it cannot read your encrypted data. – Cologler May 09 '16 at 06:38
  • Please note that until you've successfully decrypted the data, you cannot really say the encryption is working. You have to be ready for bugs at both ends. – Lasse V. Karlsen May 09 '16 at 06:52
  • Simply put, your encryption is not working. You have not flushed and properly closed the streams in the encryption method, which means you ended up not really writing all the data to the file. – Lasse V. Karlsen May 09 '16 at 06:54
  • Your decrypt method produces an error because you are using different instances of `DESCryptoServiceProvider`. You need to encrpt and decrypt using same instance, check [here](http://stackoverflow.com/a/965188/2928544) for answer. – raidensan May 09 '16 at 07:09
  • Possible duplicate of [C# - Serializing/Deserializing a DES encrypted file from a stream](http://stackoverflow.com/questions/965042/c-sharp-serializing-deserializing-a-des-encrypted-file-from-a-stream) – raidensan May 09 '16 at 07:11

3 Answers3

1

The encrypted data is probably not in the target file when you read from it. You should close your streams in EncryptFile. And since stream are disposable, you should put them in a 'using' construct:

using (fsInput=[....]) 
{
    using (fsEncrypted=[..]) 
    {
         [....]
         fsEnCrypted.Close();
    }
}
Felix D.
  • 4,811
  • 8
  • 38
  • 72
0

I think solution to Your problem is quite simple and lies in calling DecryptFile(...) method. I don't know whole code, but try to replace SaveFileDialog object with OpenFileDialog object. I can't see any SaveFileDialog object in Your attached code, so it's only rational explanation of this strange behaviour in Your app.

Hope this will be helpfull for You.

Maciej S.
  • 752
  • 10
  • 22
0

Well... just by adding flush() and close() to my cryptoStream did work. Feel free to use this lazt as f*** encryption meth-od.