Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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

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

DES Crypto

Thanks,

Kuthuparakkal
 
Share this answer
 
 
Share this answer
 
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#[^]
 
Share this answer
 
Make sure the key is the same for encryption and decryption.
 
Share this answer
 
Comments
Abdul Quader Mamun 14-Aug-12 5:40am    
easy Answer!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900