Search the Community
Showing results for tags 'encryption'.
-
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