I have to perform the encryption and decryption of the file based on des algorithm.
The encryption performed well but the decryption having the error as Bad Data when perform the readtoend function.
The coding is given below:

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.UI;
            using System.Web.UI.WebControls;
            using System.Data;
            using System.Drawing;
            using System.Text;
            using System.Runtime.InteropServices;
            using System.Security.Cryptography;
            using System.IO;

            public partial class _Default : System.Web.UI.Page
            {
                string sSecretKey;
                protected void Page_Load(object sender, EventArgs e)
                {
                }
                protected void Button1_Click(object sender, EventArgs e)
                {
                 sSecretKey = GenerateKey();
                            DecryptFile(@"E:\Encryptedd.txt", @"E:\Decrypteddd.txt", sSecretKey);
                                        // For additional security Pin the key.
                            GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
                                         // Remove the Key from memory.
                            ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
                            gch.Free(); 
                }
                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());   
        >                                                                                //the error bad data

                        fsDecrypted.Flush();
                        fsDecrypted.Close();
                    }
                [System.Runtime.InteropServices.DllImport("KERNEL32.DLL",EntryPoint = "RtlZeroMemory")]
                public static extern bool ZeroMemory(IntPtr Destination, int Length);

                                    // Function to Generate a 64 bits Key.
                static string GenerateKey()
                {
                                    // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
                    DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)
                    DESCryptoServiceProvider.Create();

                                    // Use the Automatically generated key for Encryption.
                    return ASCIIEncoding.ASCII.GetString(desCrypto.Key);

                }
            }

Please help me.

Hey so I did some looking around online. Found a post by another user somewhere else who was having a similar issue as you. Their fix

"ok..i solved this one...it turns out to be ...I need to set the padding to either zero or no padding."

However when I read your post, I had this feeling it might be white space characters doing it to you. You know like return line (\r) and new line (\n) as I have run into these as problems myself. I then found this on another site where a user was having what appeared to be a similar issue.

A bit of googling suggests that Bad Data is often caused by the decrypted data not being exactly what was output from the encryption"

Reading this just makes me feel like my suggestion to the problem might be right. So check to make sure your are not reading those white space characters like new line and return line. One suggestion that might work, I can't say for sure, is try placing a breakpoint before you write the file, and then check the content of the stream before you write it (if I am looking at your code correctly).

Try this, let me know what you get.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.