Question

I've code to decrypt file in DES Encryption, I try I keep getting the file even though I enter the key at the time when the key decrypt the encrypted differently. But i've get the error.

 while ((data = cryptostreamDecr.ReadByte()) != -1)  // Message Error : Bad Data.

Which code should I add or change in order to keep the process running decrypt?

private static void DecryptFile(string sInputFilename, string sKey)
{
    var DES = new DESCryptoServiceProvider();
    DES.Key = Encoding.ASCII.GetBytes(sKey);
    DES.IV = Encoding.ASCII.GetBytes(sKey);
    ICryptoTransform desdecrypt = DES.CreateDecryptor();
     using (var fsread = new FileStream(sInputFilename, FileMode.Open,
                FileAccess.ReadWrite))
            {
                using (var cryptostreamDecr = new CryptoStream(fsread,
                    desdecrypt,
                    CryptoStreamMode.Read))
                {
                    int data;

                    fsread.Flush();

                    using (var ms = new MemoryStream())
                    {
                        while ((data = cryptostreamDecr.ReadByte()) != -1)
                        {
                            ms.WriteByte((byte)data);
                        }

                        cryptostreamDecr.Close();

                        using (var fsWrite = new FileStream(sInputFilename, FileMode.Truncate))
                        {
                            ms.WriteTo(fsWrite);
                            ms.Flush();
                        }
                    }
                }
            }
        }

Encrypt Code :

public static void EncryptFile(string sInputFilename, string sKey)
        {
            FileStream fsInput = new FileStream(sInputFilename,
               FileMode.Open,
               FileAccess.ReadWrite);

            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsInput,
               desencrypt,
               CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            fsInput.SetLength(0);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.FlushFinalBlock();
            cryptostream.Close();
            fsInput.Close();
        }

[EDIT] : remove in Encypt :

cryptostream.FlushFinalBlock();

And add in decrypt

DES.Padding = PaddingMode.None;

Was it helpful?

Solution

64 bits is the only valid key size for the DES encryption algorithm. 8 bits is equal to one ASCII character means 64 bits is equal to 8 Characters.

If you send 8 characters only then check this (C# "Bad Data" exception when decrypting encrypted file). It may solve your problem.

[Edit] Add DES.Padding = PaddingMode.None; in DecryptFile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top