Click here to Skip to main content
14,484,535 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have my file woth encrypted format.i want to decrypt my file.i have tried like this but this is not working.

protected void Button2_Click1(object sender, EventArgs e)
       {
           DecryptFile(@"d:\eula.3082.txt", @"d:\sql\eula.3082.txt", GenerateKey());
       }

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());
            fsDecrypted.Flush();
            fsDecrypted.Close();
        }




In this line i am getting error: Bad data

fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
Posted
Updated 13-Aug-12 23:56pm
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

Make sure the key is the same for encryption and decryption.
   
Comments
Abdul Quader Mamun 14-Aug-12 5:40am
   
easy Answer!
Rate this:
Please Sign up or sign in to vote.

Solution 2

I think your problem may be the key :

GenerateKey()

If you are generating a random key you will get an exception becuase a file needs to be decrtypted with the same key it was encrypted.
unless you are creating an application that is supposed to hack the key using a dictionary or brite force attack , but even though the algorithm isnt the strongest if they key is long that will take a while.

Also can we see the encrypt command , did you encrypt the file . Make sure you are using the exact same algorithm and encoding that encrypted it.

Heres a brilliant article to get you started on encryption and decryption.

Simple encrypting and decrypting data in C#[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

   
Rate this:
Please Sign up or sign in to vote.

Solution 4

Try this:
I tried even .exe files encrypt/decrypt works, .exe works after decrypted.

DES Crypto

Thanks,

Kuthuparakkal
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100