Flushfinalblock Bad Data Error
Contents |
here for a quick overview of the 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
Cryptostream Flushfinalblock Bad Data
this site About Us Learn more about Stack Overflow the company Business Learn more about hiring vb.net flushfinalblock bad data 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
Bad Data Exception Java
a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Occasional Bad Data Error while decrypting the string : System.Security.Cryptography.CryptographicException up vote 4 down vote favorite 2 In my ASP.NET WebForms App (The app is running on windows server 2008 R2, IIS bad data error in c# 7.5 and Runtime v4.0 Integrated Mode App Pool if it matters), I am encrypting data, putting it on the QueryString and decrypting data using System.Security.Cryptography.SymmetricAlgorithm class. But I am occasionally having some problems with decrypting the data I am getting the following exception; Bad Data. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Security.Cryptography.CryptographicException: Bad Data. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [CryptographicException: Bad Data. ] System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +33 System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone) +0 System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) +313 System.Security.Cryptography.CryptoStream.FlushFinalBlock() +33 Cryptography35.SymmetricEncryptionUtility.DecryptData(Byte[] data, String keyFile) in E:\Documents\@Library\Cryptography35\Cryptography35\SymmetricEncryptionUtility.cs:124 Cryptography35.SymmetricQueryString.SymmetriclyE
to 3 of 3 Thread: Bad Data Error - Decryptor LinkBack LinkBack URL About LinkBacks Bookmark & Share Digg this Thread!Add Thread to del.icio.usBookmark in TechnoratiTweet this thread Thread Tools Show Printable Version Search Thread Advanced Search Display Linear Mode Switch to Hybrid Mode http://stackoverflow.com/questions/6555887/occasional-bad-data-error-while-decrypting-the-string-system-security-cryptogr Switch to Threaded Mode 05-24-2014,3:01 AM #1 pidyok VB.NET Forum Newbie .NET Framework.NET 3.5 (VS 2008) Join Date Feb 2009 Posts 19 Reputation 96 Bad Data Error - Decryptor The following encrypting/decrypting code works well during saving/opening a text file/stream. But when http://www.vbdotnetforums.com/security/60482-bad-data-error-decryptor.html I close and re-launch my the application, open the previously saved text file through streamreader, and during decryption, error is encountered at decStream.FlushFinalBlock() line saying "Bad Data"... Would appreciate any help, please... thanks! Code: Sub iSave_WB() Dim ioFile As New StreamWriter(iFileName) With ioFile .WriteLine(EncryptDataTxt("cboWBMode,0")) .WriteLine(EncryptDataTxt("dtpActivity," & Format(dtpActivity.Value, "yyyy-MM-dd"))) .WriteLine(EncryptDataTxt("cboRegistration," & cboRegistration.SelectedItem.Col1)) End With ioFile.Close() ioFile.Dispose() End Sub Private Function DecryptDataTxt(ByVal encryptedTxt As String) As String Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedTxt) Dim mStr As New System.IO.MemoryStream Dim decStream As New CryptoStream(mStr, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write) decStream.Write(encryptedBytes, 0, encryptedBytes.Length) decStream.FlushFinalBlock() 'Error is encountered here... decStream.Close() Return System.Text.Encoding.Unicode.GetString(mStr.ToArray) End Function Private Function EncryptDataTxt(ByVal plainText As String) As String Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext) Dim mStr As New System.IO.MemoryStream Dim encStream As New CryptoStream(mStr, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStre
Runtime Internals and Architecture Question 0 Sign in to vote https://social.msdn.microsoft.com/Forums/vstudio/en-US/b64250af-e322-4614-b9d1-646d8d358644/bad-data-error-with-decryption?forum=clr I'm using TripleDESCryptoServiceProvider to encrypt and decrypt Xml files that are generated on the fly in two different places.In one part I'm allowing a user http://www.pcreview.co.uk/threads/cryptographic-exception-bad-data-descryptoserviceprovider.1227899/ to download file, an XmlWriter writes to my CryptoStream which writes straight to the Response.OutputStream. Before any writing I call the following Response.Clear(); Response.AddHeader("content-disposition","attachment; bad data filename=myXml.xml"); Response.ContentType = "text/xml"; Response.BufferOutput = true;The browser prompts for download, and everything is fine.I can then upload the file and have an XmlReader read from my CryptoStream which reads from FileUploader.PostedFile.InputStream and everything works fine.Now in the other place, I have an XmlWriter write to my CryptoStream which in flushfinalblock bad data turn writes to a MemoryStream. After I flush the writer, I set the memory stream position back to 0, and then use it for an attatchment in an email. Attachment at = new Attachment(memStream, "myXml.xml", "text/xml"); MailMessage.Attachments.Insert(0, at);I send the email and everything works fine.When I save the file and try to upload it, it goes through the exact same upload process.The XmlReader begins to read the from the CryptoStream and is able to begin reading the file. Towards the end of the file, the Reader tries to read and I get the following:{"Bad Data.\r\n"} System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) at System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone) at System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Xml.XmlTextReaderImpl.ReadData() at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr) at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at System.Xml.XmlTextReaderImpl.Read()
Guest I have read that a similar problem to the one I describe below has been discussed on the microsoft.public.dotnet.security newsgroup, but I was unable to see a solution to the problem with which I am struggling. I am hoping that someone reading here will be able to assist me. As far as I can tell, I'm doing exactly what the article at http://www.dotnetthis.com/Articles/Crypto.htm does, but I keep getting this exception. If someone could assist or point me to a good, relevant article, that would be great. Here's a snippet of code that demonstrates my problem: ------------------------------------------------------------------------- // Input - actually obtained from output of previous encryption byte [] atvyData = new byte [8] {0xE4, 0x83, 0x16, 0xD4, 0x89, 0x47, 0xAE,
0x68}; // Encryption key and initialization vector to use byte [] atvyKey = new byte [8] {0xFA, 0xFA, 0xFA, 0xFA, 0xFA, 0xFA, 0xFA,
0xFA}; byte [] atvyIV = new byte [8] {0, 0, 0, 0, 0, 0, 0, 0}; // Decrypt MemoryStream tvOutput = new MemoryStream(); DESCryptoServiceProvider tvDESAlg = new DESCryptoServiceProvider(); CryptoStream tvCryptStream = new CryptoStream(tvOutput, tvDESAlg.CreateDecryptor(atvyKey, atvyIV), CryptoStreamMode.Write); tvCryptStream.Write(atvyData, 0, 8); tvCryptStream.FlushFinalBlock(); byte [] atvyResult = tvOutput.ToArray(); ------------------------------------------------------------------------- Every time I execute the 'tvCryptStream.FlushFinalBlock()' I get the 'Bad Data' exception - why? I have tried numerous tweaks and variations to the code above and have been unable to get rid of the exception. Any help would be greatly appreciated! Thanks, Tyler Tyler, Feb 12, 2004 #1 Advertisements Jon Skeet [C# MVP] Guest Tyler <> wrote: > I have read that a similar problem to the one I describe below has been > discussed on the microsoft.public.dotnet.security newsgroup, but I was > unable to see a solution to the problem with which I am struggling. I am > hoping that someone reading here will be able to assist me. > > As far as I can tell, I'm doing exactly what the article at > http://www.dotnetthis.com/Articles/Crypto.htm does, but I keep getting this > exception. If someone could assist or point me to a good, relevant article, > that would be great. > > Here's a snippet of code that demonstrates my problem: