Try changing this:

Dim decryptedBytes(Convert.ToInt32(reader.Length)) As Byte

to this:

Dim decryptedBytes(Convert.ToInt32(reader.Length)-1) As Byte

I've seen this happen in VB.Net because when you create the byte array, the
value you pass is the upper bound, not the capacity.  So, you get a byte
array that is bigger than the data you encrypted.

Most often, when I get a "Bad Data" error, it is usually because of some
problem with array sizes not matching either from what I just mentioned, or
from not flushing properly on the encrypt.

Now, if this same code runs twice and only fails the second time (I got a
bit confused on your explanation) then this is probably not the issue, but I
can't see anything that would cause a difference between the first and
second iteration.

HTH,
Matt

-----Original Message-----
From: Marina
To: [EMAIL PROTECTED]
Sent: 4/23/02 2:42 PM
Subject: [DOTNET] Cryptographic Exception

Hi all,

Here is the scenario. I have an asp.net page, that uses remoting to
invoke a COM+ object elsewhere. The remote object it invokes, is
actually a wrapper for the COM+ object, and is hosted in IIS. Here is
some code that it executes:

Dim reader As System.IO.FileStream
Dim decryptorStream As CryptoStream
reader = New System.IO.FileStream("c:\myfile.enc",
System.IO.FileMode.Open)
Dim cryptoProvider As DESCryptoServiceProvider = New
DESCryptoServiceProvider()
decryptorStream = New CryptoStream(reader, _
cryptoProvider.CreateDecryptor(cCryptokey, cCryptokey), _
CryptoStreamMode.Read)
Dim decryptedBytes(Convert.ToInt32(reader.Length)) As Byte
decryptorStream.Read(decryptedBytes, 0, Convert.ToInt32(reader.Length))
decryptorStream.Close()
reader.Close()

The first time I load the asp.net page, this works fine, I step through
it, everything is great.  If I then reload the page, which calls the
method that contains this code, I get a CryptographicException with the
message "Bad Data" on the "decryptorStream.Read" line, which tells me
nothing. The first time this code executes, the contents of the file
seem to be acceptable, but not any other times. If I shut down the
asp.net process, the first time page loads fine, if I reload, this
happens.
Any ideas as to why this is happening?

Thanks,
Marina

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to