[quoted text, click to view] On Wed, 13 Feb 2008 21:09:36 -0800, Gasnic <gasnic@gmail.com> wrote:
> I pass a literal string into the CryptoStream to encrypt the data into a
> MemoryStream. And then decrypt the MemoryStream back. But I am getting
> CryptographicException with Bad Data.
>
> When I tried to encryption the same data into a FileStream and then
> decrypt the file, it works.
>
> [...]
> I used StreamWriter to write data into the CryptoStream but I didn't
> specify the encoding.
> Should I specify an encoding? if so, which encoding should I use?
I would not expect how you stream data to the CryptoStream to affect the
encryption. You could very well run into an encoding issue when trying to
reconstitute the text data from the _unencrypted_ stream but a) unless
you're changing the encoding I would expect the default encoding to apply
in both directions, and b) the encoding shouldn't have any effect on the
data after it's encrypted, nor should it affect the decryption of the
previously encrypted data. You might get an exception thrown by a
StreamReader or something trying to read the unencrypted stream, but it
shouldn't affect the encryption itself.
This all assumes, of course, that when you write "I used StreamWriter to
write data into the CryptoStream", you are talking about the encryption
stage and that your data flow looks something like:
StreamWriter --> CryptoStream --> FileStream
and
StreamWriter --> CryptoStream --> MemoryStream
for encryption, and basically the inverse for decryption (with the
appropriate stream classes replaced as necessary of course).
If you're using StreamWriter post-encryption, then yes...that'd be a
problem. :)
Generally speaking, what you're trying to do (if I understand the post
correctly) should work. You should be able to use a MemoryStream and
FileStream essentially interchangeably here and they should work the
same. Since they are not working the same, I suspect that you've got some
sort of simple typographical error or you're not treating the two exactly
the same somehow.
You should look through the code carefully to make sure you're not
treating the two different. You may even want to make sure that the code
doing the encryption only ever uses a Stream that you pass to it for
output and input, and just make the caller aware of the MemoryStream
versus FileStream. That will guarantee that at least the encryption part
isn't depending on the type of the stream used to store the encrypted data.
If after checking all that you still have a problem, you should post a
concise-but-complete code sample that reliably demonstrates the problem.
The exception you describe seems to imply that the data isn't making the
round-trip post-encryption/pre-decryption safely, but if you're really
treating both storage streams the same, it's hard to see how that might
happen.
One final thought (now that I've written all of the above, this occurs to
me :) ): are you sure that in the MemoryStream case, you have reset the
stream position back to the beginning before trying to decrypt the data?