Encrypting\Decrypting
-
I am using the following to open and write and encryped datafile. FileStream fout = new FileStream(OptionsDataFile, FileMode.Create, FileAccess.Write); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Write); ASCIIEncoding AE = new ASCIIEncoding(); and the following to decrypt. FileStream fin = new FileStream(OptionsDataFile, FileMode.Open, FileAccess.Read); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fin, tdes.CreateDecryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Read); ASCIIEncoding AE = new ASCIIEncoding(); int tempByte = new int(); byte[] inBytes = new byte[(int)fin.Length]; encStream.Read(inBytes,0,(int)fin.Length); When it gets to the read I get the following. An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional information: Bad Data. Anyone have any ideas why. Everything is the same for the write. I just do not get why the data would be bad.
-
I am using the following to open and write and encryped datafile. FileStream fout = new FileStream(OptionsDataFile, FileMode.Create, FileAccess.Write); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Write); ASCIIEncoding AE = new ASCIIEncoding(); and the following to decrypt. FileStream fin = new FileStream(OptionsDataFile, FileMode.Open, FileAccess.Read); TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fin, tdes.CreateDecryptor(new byte[] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125} ,new byte[] {216,237,102,42,142,11,133,104,}), CryptoStreamMode.Read); ASCIIEncoding AE = new ASCIIEncoding(); int tempByte = new int(); byte[] inBytes = new byte[(int)fin.Length]; encStream.Read(inBytes,0,(int)fin.Length); When it gets to the read I get the following. An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Additional information: Bad Data. Anyone have any ideas why. Everything is the same for the write. I just do not get why the data would be bad.
Not sure what's going on for you. Maybe it's something in the code you *don't* show . The following works just fine for me (I separated out the creation of some the objects into separate statements for readability):
class Class1 {
static void Encrypt(string inputFile, string outputFile) {
FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write); byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125}; byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104}; TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); ICryptoTransform t = tripleDes.CreateEncryptor(key, iv); CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write); int len = (int) fsin.Length; byte\[\] buffer = new byte\[len\]; len = fsin.Read(buffer, 0, len); cs.Write(buffer, 0, len); cs.Close(); fsout.Close(); fsin.Close(); } static void Decrypt(string inputFile, string outputFile) { FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read); FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write); byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125}; byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104}; TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); ICryptoTransform t = tripleDes.CreateDecryptor(key, iv); CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write); int len = (int) fsin.Length; byte\[\] buffer = new byte\[len\]; len = fsin.Read(buffer, 0, len); cs.Write(buffer, 0, len); cs.Close(); fsout.Close(); fsin.Close(); } static void Main(string\[\] args) { Console.WriteLine("Starting encryption..."); Encrypt(@"c:\\test.txt", @"c:\\test.enc"); Console.WriteLine("...done"); Console.WriteLine("Starting decryption..."); Decrypt(@"c:\\test.enc", @"c:\\test.dec"); Console.WriteLine("...done"); Console.ReadLine(); }
}
Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.
-
Not sure what's going on for you. Maybe it's something in the code you *don't* show . The following works just fine for me (I separated out the creation of some the objects into separate statements for readability):
class Class1 {
static void Encrypt(string inputFile, string outputFile) {
FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write); byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125}; byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104}; TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); ICryptoTransform t = tripleDes.CreateEncryptor(key, iv); CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write); int len = (int) fsin.Length; byte\[\] buffer = new byte\[len\]; len = fsin.Read(buffer, 0, len); cs.Write(buffer, 0, len); cs.Close(); fsout.Close(); fsin.Close(); } static void Decrypt(string inputFile, string outputFile) { FileStream fsin = new FileStream(inputFile, FileMode.Open, FileAccess.Read); FileStream fsout = new FileStream(outputFile, FileMode.Create, FileAccess.Write); byte\[\] key = new byte\[\] {22,184,62,104,66,100,202,17,91,254,197,181,224,62,36,138,128,227,146,111,134,83,241,125}; byte\[\] iv = new byte\[\] {216,237,102,42,142,11,133,104}; TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider(); ICryptoTransform t = tripleDes.CreateDecryptor(key, iv); CryptoStream cs = new CryptoStream(fsout, t, CryptoStreamMode.Write); int len = (int) fsin.Length; byte\[\] buffer = new byte\[len\]; len = fsin.Read(buffer, 0, len); cs.Write(buffer, 0, len); cs.Close(); fsout.Close(); fsin.Close(); } static void Main(string\[\] args) { Console.WriteLine("Starting encryption..."); Encrypt(@"c:\\test.txt", @"c:\\test.enc"); Console.WriteLine("...done"); Console.WriteLine("Starting decryption..."); Decrypt(@"c:\\test.enc", @"c:\\test.dec"); Console.WriteLine("...done"); Console.ReadLine(); }
}
Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.