Home > bad data > bad data error in c#

Bad Data Error In C#

Contents

here for a quick overview of the site Help Center Detailed answers to any questions you might

C# Des Decrypt Bad Data

have Meta Discuss the workings and policies of this site About c# rsacryptoserviceprovider decrypt bad data Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting bad data exception c# ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million

The Stub Received Bad Data C#

programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C# “Bad Data” exception when decrypting encrypted file up vote 1 down vote favorite 1 Hey I'm very new to encryption and decryption, or even the c# language to be honest. Basically, I have a tcp chat server that

Data Error Event In Datagridview C#

"saves" logs and encrypts the text file. This is how I encrypt (based from the MSDN sample): public static void EncryptFile(string strInputFileName, string strOutputFileName, string strKey) { FileStream fsIn = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read); FileStream fsOut = new FileStream(strOutputFileName, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Key = ASCIIEncoding.ASCII.GetBytes(strKey); des.IV = ASCIIEncoding.ASCII.GetBytes(strKey); ICryptoTransform desencrypt = des.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsOut, desencrypt, CryptoStreamMode.Write); byte[] byteArrayInput = new byte[fsIn.Length - 1]; fsIn.Read(byteArrayInput, 0, byteArrayInput.Length); cryptostream.Write(byteArrayInput, 0, byteArrayInput.Length); fsIn.Close(); fsOut.Close(); } The method success fully encrypts files. This is my decrypt method: public static void DecryptFile(string strInputFileName, string strOutputFileName, string strKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Key = ASCIIEncoding.ASCII.GetBytes(strKey); des.IV = ASCIIEncoding.ASCII.GetBytes(strKey); byte[] te = new byte[1024]; FileStream fsRead = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read); ICryptoTransform desdecrypt = des.CreateDecryptor(); CryptoStream cryptostream = new CryptoStream(fsRead, desdecrypt, CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(strOutputFileName); fsDecrypted.Write(new StreamReader(cryptostream).ReadToEnd());//This is where the "Bad Data" occurs. fsDecrypted.Flush(); fsDecrypted.Close(); fsRead.Close(); } And when I inspect the cryptostream object, it says that it ha

here for a quick overview of the site Help Center Detailed answers to c# datagridview handle data error any questions you might have Meta Discuss the workings and policies

System.security.cryptography.cryptographicexception Bad Data C#

of this site About Us Learn more about Stack Overflow the company Business Learn more flushfinalblock bad data about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack http://stackoverflow.com/questions/5591361/c-sharp-bad-data-exception-when-decrypting-encrypted-file Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Bad data exception in C# up vote -1 down vote favorite i have a "bad data exception" when trying to decrypt a file using DES in C#.Any help http://stackoverflow.com/questions/15707458/bad-data-exception-in-c-sharp would be great.here is my code: namespace encrypte { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static 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.Mode = CipherMode.CFB; DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); DES.Padding = PaddingMode.ISO10126; 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); cryptostream.FlushFinalBlock(); fsInput.Close(); fsEncrypted.Close(); } static void DecryptFile(string sInputFilename,string sOutputFilename,string sKey) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); DES.Mode = CipherMode.CFB; DES.Padding = PaddingMode.ISO10126; FileStream fsread = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt,CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } private void button1_Click(object sender, EventArgs e) { EncryptFile(@"E:\a.wmv", @"E:\b.wmv", "abcdefgh"); } private void button2_Click(object sender, EventArgs e) { DecryptFile(@"E:\b.wmv", @"E:\

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About http://stackoverflow.com/questions/22423412/getting-bad-data-exception-during-decryption-using-net-cryptography Us Learn more about Stack Overflow the company Business Learn more about hiring http://stackoverflow.com/questions/6107288/des-decryption-error-bad-data-in-c-sharp-when-closing-cryptostream developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Getting Bad Data exception during decryption bad data (using .Net Cryptography) up vote 0 down vote favorite I am using symmetric TripleDES encryption from the class TripleDESCryptoServiceProvider (.Net 2.0) to encrypt the contents of a file. The data gets encrypted perfectly but during decryption it throws the CryptographyException: Bad data I am using key from current date time and IV from the randomly generated value by .Net class. Then I pass the same key decrypt bad data and IV to the decryption method but it fails for some reason. Here is my code : static void Main(string[] args){ string fileName = "input.exe"; string newFileName = fileName + "crypted.exe"; byte[] iv; var fileToEncrypt = File.ReadAllBytes(fileName); var encryptionKey = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(DateTime.Now.ToString("ddMMyyHmmss"))); File.WriteAllBytes(newFileName, EncryptTripleDES(fileToEncrypt, encryptionKey, out iv)); File.WriteAllBytes(fileName + "decrypted.exe", DecryptTripleDES(File.ReadAllBytes(newFileName), encryptionKey,iv)); } public static byte[] EncryptTripleDES(byte[] dataToEncrypt, byte[] key, out byte[] iv){ byte[] result; var tdes = new TripleDESCryptoServiceProvider { Key = key, KeySize = 128, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; iv = tdes.IV; using(ICryptoTransform cTransform = tdes.CreateEncryptor()){ result = cTransform.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length); tdes.Clear(); } return result; } public static byte[] DecryptTripleDES(byte[] dataToDecrypt, byte[] key, byte[] iv){ byte[] result; var tdes = new TripleDESCryptoServiceProvider { Key = key, KeySize = 128, Mode = CipherMode.CBC,IV = iv,Padding = PaddingMode.PKCS7 }; using (ICryptoTransform cTransform = tdes.CreateDecryptor()){ result = cTransform.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length); tdes.Clear(); } return result; } UPDATE: 1. I have checked the value of IV being passed is valid and same as used in encryption. 2. Changing the padding to zero or none doesn't raise the exception but then the data isn't decrypted correctly. It comes out to be different than original. c# .net encryption cryptography share|

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up DES decryption error “Bad Data” in C# when closing cryptostream up vote 0 down vote favorite I try to decrypt an encrypted byte array (encrypt with K1 and decrypt with K2). Visual Studio throws an exception "BAD DATA" when it tries to close my crypto stream here's my code snippet of DES decryption public Byte[] Decrypt(Byte[] cipherData, Byte[] key, Byte[] iv) { MemoryStream ms = new MemoryStream(); DES mDES = DES.Create(); mDES.Key = key; mDES.IV = iv; mDES.Padding = PaddingMode.PKCS7; CryptoStream cs = new CryptoStream(ms, mDES.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(cipherData, 0, cipherData.Length); cs.Close(); Byte[] decryptedData = ms.ToArray(); return decryptedData; } the initial vector is the same as encryption. I don't know why this error occurred. Added: As recommended by Greg B, I post here my code snippet of encryption. The output of encryption is the input of decryption (two different keys) public Byte[] Decrypt(Byte[] cipherData, Byte[] key, Byte[] iv) { MemoryStream ms = new MemoryStream(); DES mDES = DES.Create(); mDES.Key = key; mDES.IV = iv; mDES.Padding = PaddingMode.PKCS7; CryptoStream cs = new CryptoStream(ms, mDES.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(cipherData, 0, cipherData.Length); cs.Close(); Byte[] decryptedData = ms.ToArray(); return decryptedData; } c# .net encryption des share|improve this question edited May 24 '11 at 8:11 asked May 24 '11 at 7:49 Hai-Binh LE 107110 If it's throwing an exception when decrypting, maybe there is something wrong with your encryption routine. Might be worth posting that code too... –Greg B May 24 '11 at 8:01 Look here: stackoverflow.com/questions/5591361/… –Alireza Maddah May 24 '11 at 8:09 I had a look at the link suggested by Alireze but it seems to me that it doesn't resolve my problem :( –Hai-Binh LE May 24 '11 at

 

Related content

bad data crc error cannot get kernel image

Bad Data Crc Error Cannot Get Kernel Image p Hi I have burned u-boot uImage and qtopia M in my nand While booting it shows me this and gets stuck at the end saying Bad Data CRC ERROR can't get kernel image This is the output relatedl on minicom Please let me know the solution to this problem U-Boot -mini May - I C ready DRAM MB Flash MB NAND Bad block table not found for chip Bad block table not found for chip MiB Found Environment offset in OOB USB S C USB Deviced In serial Out serial Err

bad data error

Bad Data Error table id toc tbody tr td div id toctitle Contents div ul li a href Examples Of Bad Data a li li a href Bad Data Band a li ul td tr tbody table p here for a quick overview of the site relatedl Help Center Detailed answers to any questions you cryptostream bad data error might have Meta Discuss the workings and policies of this decstream flushfinalblock bad data site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or cryptostream read bad data posting ads with us Stack Overflow

bad data crc error

Bad Data Crc Error table id toc tbody tr td div id toctitle Contents div ul li a href U Boot Crc Error a li ul td tr tbody table p Hi I have burned u-boot uImage and qtopia M in my nand While booting it shows me this and gets stuck at the end saying Bad Data CRC ERROR can't get kernel image This is the output on minicom Please let me know relatedl the solution to this problem U-Boot -mini May - verifying checksum bad data crc u boot I C ready DRAM MB Flash MB NAND Bad

bad data error asp.net

Bad Data Error Asp net p here for a quick overview of the site Help Center Detailed answers to any questions you might have relatedl Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of million programmers just like you helping each other Join them it only takes a minute Sign up ldquo Bad Data rdquo CryptographicException up

cryptostream bad data error

Cryptostream Bad Data Error table id toc tbody tr td div id toctitle Contents div ul li a href Bad Data Exception Java a li li a href Vb net Flushfinalblock Bad Data a li li a href C Rsa Decrypt Bad Data a li ul td tr tbody table p here for a quick overview of relatedl the site Help Center Detailed answers to any system security cryptography cryptographicexception bad data c questions you might have Meta Discuss the workings and policies bad data cryptographicexception c of this site About Us Learn more about Stack Overflow the company Business

error bad data line

Error Bad Data Line table id toc tbody tr td div id toctitle Contents div ul li a href Gnuplot Bad Data On Line a li li a href Gnuplot Csv a li li a href Gnuplot Using a li ul td tr tbody table p p p p p p p p

error while writing encrypted data stream

Error While Writing Encrypted Data Stream table id toc tbody tr td div id toctitle Contents div ul li a href Cryptostream Flushfinalblock Bad Data a li li a href Bad Data Exception C a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have relatedl Meta Discuss the workings and policies of this site system security cryptography cryptographicexception bad data c About Us Learn more about Stack Overflow the company Business Learn more about bad data exception java hiring developers or posting ads with

flushfinalblock bad data error

Flushfinalblock Bad Data Error table id toc tbody tr td div id toctitle Contents div ul li a href Cryptostream Flushfinalblock Bad Data a li li a href Bad Data Exception Java a li ul td tr tbody table p here for a quick overview of the relatedl site Help Center Detailed answers to any questions system security cryptography cryptographicexception bad data c you might have Meta Discuss the workings and policies of p h id Cryptostream Flushfinalblock Bad Data p this site About Us Learn more about Stack Overflow the company Business Learn more about hiring vb net flushfinalblock