Jibrohni Posted August 26, 2010 Posted August 26, 2010 Good day one and all, Apologies if I'm not in the most suitable topic area for this. Can someone tell me why I'm getting a bad data error when I try using this decryption method? The encryption part works fine, but when decrypting the file I get the error: CryptographicExcpetion was unhandled Bad Data on the line fsDecrypted.Write(sr.ReadToEnd()) please see the code I'm trying to use: /// <summary> /// Encrypts a file - Not implemented /// </summary> /// <param name="sInputFilename">File to encrypt</param> /// <param name="sOutputFilename">Encrypted file name</param> /// <param name="sKey">Encryption Key</param> private void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) { FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(sOutputFilename, 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); fsEncrypted.Close(); } /// <summary> /// Decrypts a file - Not implemented /// </summary> /// <param name="sInputFilename">File to decrypt</param> /// <param name="sOutputFilename">Decrypted file name</param> /// <param name="sKey">Decryption Key</param> 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); StreamReader sr = new StreamReader(cryptostreamDecr); //Print the contents of the decrypted file. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(sr.ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } I'm using the same key for both "m43%gj&^", and the fact that it encrypts suggests that that is fine to use. Thanks in advance for your time. Jib Quote
joe_pool_is Posted January 27, 2011 Posted January 27, 2011 Hey Jib, I did an encryption job about 2 years ago. (see attached) I opened the project, had to update it to VS 2008, and verified it still worked. I don't remember why I did all that I did, but I did not need the DESCryptoServiceProvider class. Feel free to poke around my code. It should still work as is. The security key will be missing when you first run it, but the code will prompt you to create a new one and go from there. Hope it helps.jp2crypt.zip Quote Avoid Sears Home Improvement
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.