473,791 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with CryptoStream and incomplete files...

Ok, with the help of some examples found on the web and some minor
modifications on our own, we have a simple and working encrypt and
decrypt solution. It runs as a service, watches for files with a
specific extension in a specific directory. The files are uploaded by
FTP to this directory. The service then does the following steps:

1) Verify it can open the file (so we know it's fully uploaded).
2) Try to decrypt the file with known keys
3) DRM the file
4) Move the file to an another directory for download at a later time.
This works great. As long as the file gets completely uploaded. If
the file is a partial file, the CryptoStream we have open to the file
dies out at near the end of the file with the following error
(gathered from our logfile):

------
Length of the data to decrypt is invalid. | TransformFinalB lock |
System.Security .Cryptography.C ryptographicExc eption: Length of the
data to decrypt is invalid.
at
System.Security .Cryptography.C ryptoAPITransfo rm.TransformFin alBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security .Cryptography.C ryptoStream.Flu shFinalBlock()
at System.Security .Cryptography.C ryptoStream.Clo se()
------

What I'd like to be able to do is to delete that file either before
the CryptoStream is used if there is some way to tell it's an
incomplete file or to delete the file once I get the above exception.
The problem is, I can't access the file. I've tried
CryptoStream.Cl ose, CryptoStream.Cl ear, etc, nothing gets me access to
that file again until the service is stopped.

I've included some code below, which is a partial code listing along
with lots of calls to UpdateStatus (which is just updated a log file
with the time and the message) as I was trying to track down this
problem.

Any help anyone could offer would be GREAT.

Thanks much,

M

This a portion of the main function that runs when a file is found in
the directory:

----------------------------------
Private Function ProcessFile(ByV al file_name As String) As Boolean
Dim tempPath As String
Dim tempName As String
Dim testStream As FileStream

'Update Status
UpdateStatus("P rocessing: " & file_name)

'Try opening if the file, if it fails, start the timer.
Try
testStream = New FileStream(file _name, FileMode.Open,
FileAccess.Read )
testStream.Clos e()
Catch ex As System.IO.IOExc eption
UpdateStatus("U nable to Open File: (expected error): " &
ex.Message)
timeRetry.Enabl ed = True
Return False
Catch ex As Exception
UpdateStatus("U nexpected Error Occured: " & ex.Message)
timeRetry.Enabl ed = True
Return False
End Try

'Decrypt File
UpdateStatus("D ecrypting: " & file_name)
tempPath = file_name.Subst ring(0, file_name.LastI ndexOf("."))
& ".wmv"
tempName = tempPath.Substr ing(tempPath.La stIndexOf("\") + 1,
tempPath.Length - (tempPath.LastI ndexOf("\") + 1))
Try
CryptFile(m_Pas sword, file_name, tempPath, False)
Catch ex As Exception
UpdateStatus("D ecrypting Error: " & ex.Message & " | " &
ex.TargetSite() .Name & " | " & ex.ToString)
'If we can't encode the file, it's no good to us and we
delete it.
Try
UpdateStatus("D ecrypting Error: REMOVING INVALID
FILE!")
If Not crypto_stream Is Nothing Then
UpdateStatus("M ANUALLY DOING MY THING!")
crypto_stream.C lose()
crypto_stream = Nothing
End If
System.IO.File. Delete(file_nam e)
System.IO.File. Delete(tempPath )
Catch ex1 As Exception
UpdateStatus("D ecrypt clean up error: " & ex1.Message)
End Try
Return True
End Try
[DRM AND TEMP FILE DELETE HAPPENS AFTER HERE]
---------------------------------------------------------------------

This is the cryptfile function:
(in this code, crypto_stream is defined as a global, so I could get
access to the handle back in the main function, just as another shot
at closing is properly... it's defined as "private crypto_stream as
CryptoStream"
------------------------------------------------
Private Sub CryptFile(ByVal password As String, ByVal in_file As
String, ByVal out_file As String, ByVal encrypt As Boolean)
' Create input and output file streams.
Dim in_stream As FileStream
Dim out_stream As FileStream
Dim des_provider As New TripleDESCrypto ServiceProvider
Dim block_size_bits As Integer
Dim key As Byte() = Nothing
Dim iv As Byte() = Nothing
Dim crypto_transfor m As ICryptoTransfor m
Const BLOCK_SIZE As Integer = 1024
Dim buffer(BLOCK_SI ZE) As Byte
Dim bytes_read As Integer
UpdateStatus("C RYPTFILE!")
Try
in_stream = New FileStream(in_f ile, FileMode.Open,
FileAccess.Read )
out_stream = New FileStream(out_ file, FileMode.Create ,
FileAccess.Writ e)
Catch ex As Exception
UpdateStatus("F irst CLOSING")
If Not in_stream Is Nothing Then
in_stream.Close ()
in_stream = Nothing
End If
If Not out_stream Is Nothing Then
out_stream.Clos e()
out_stream = Nothing
End If
Throw ex
End Try

' Encrypt or decrypt the file.
Try
' Find a valid key size for this provider.
Dim key_size_bits As Integer = 0
For i As Integer = 1024 To 1 Step -1
If des_provider.Va lidKeySize(i) Then
key_size_bits = i
Exit For
End If
Next i
UpdateStatus("K ey Size: " & key_size_bits)
Debug.Assert(ke y_size_bits > 0)

' Get the block size for this provider.
UpdateStatus("B lock Size: " & block_size_bits )
block_size_bits = des_provider.Bl ockSize

' Generate the key and initialization vector.
UpdateStatus("M ake Key and IV!")
MakeKeyAndIV(pa ssword, key_size_bits, block_size_bits ,
key, iv)

UpdateStatus("S etting to Decrypt")
' Make the encryptor or decryptor.
If encrypt Then
crypto_transfor m = des_provider.Cr eateEncryptor(k ey,
iv)
Else
crypto_transfor m = des_provider.Cr eateDecryptor(k ey,
iv)
End If

' Attach a crypto stream to the output stream.
UpdateStatus("A ttaching the crypto stream")
crypto_stream = New CryptoStream(ou t_stream,
crypto_transfor m, CryptoStreamMod e.Write)

UpdateStatus("A t the Do loop")
Do
Try
' Read some bytes.
UpdateStatus("R ead Some Bytes")
bytes_read = in_stream.Read( buffer, 0, BLOCK_SIZE)
If bytes_read = 0 Then
UpdateStatus("R ead 0 Bytes")
If Not crypto_stream Is Nothing Then
crypto_stream.C lose()
crypto_stream = Nothing
End If
If Not in_stream Is Nothing Then
in_stream.Close ()
in_stream = Nothing
End If
If Not out_stream Is Nothing Then
out_stream.Clos e()
out_stream = Nothing
End If
Exit Do
End If

' Write the bytes into the CryptoStream.
UpdateStatus("E ncrypting " & bytes_read & "
bytes")
crypto_stream.W rite(buffer, 0, bytes_read)
Catch ex As Exception
' Close the streams.
UpdateStatus("S econd CLOSING")
If Not crypto_stream Is Nothing Then
crypto_stream.C lose()
crypto_stream = Nothing
End If
If Not in_stream Is Nothing Then
in_stream.Close ()
in_stream = Nothing
End If
If Not out_stream Is Nothing Then
out_stream.Clos e()
out_stream = Nothing
End If

Throw ex
End Try
Loop
Catch ex As Exception
' Close the streams.
UpdateStatus("T hird CLOSING")
If Not crypto_stream Is Nothing Then
crypto_stream.C lose()
crypto_stream = Nothing
End If
If Not in_stream Is Nothing Then
in_stream.Close ()
in_stream = Nothing
End If
If Not out_stream Is Nothing Then
out_stream.Clos e()
out_stream = Nothing
End If

Throw ex
End Try

' Close the streams.
UpdateStatus("L ast CLOSING")
crypto_stream.C lose()
in_stream.Close ()
out_stream.Clos e()
End Sub
-------------------------------------------
Log file of what happens to file:
------------------------------
[6/9/2005 8:04:31 AM] Starting
[6/9/2005 8:04:32 AM] Processing:
C:\Inetpub\wwwr oot\Vivid\video \33291394.xxx
[6/9/2005 8:04:32 AM] Decrypting:
C:\Inetpub\wwwr oot\Vivid\video \33291394.xxx
[6/9/2005 8:04:32 AM] CRYPTFILE!
[6/9/2005 8:04:32 AM] Key Size: 192
[6/9/2005 8:04:32 AM] Block Size: 0
[6/9/2005 8:04:32 AM] Make Key and IV!
[6/9/2005 8:04:33 AM] Setting to Decrypt
[6/9/2005 8:04:33 AM] Attaching the crypto stream
[6/9/2005 8:04:33 AM] At the Do loop
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 1024 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Encrypting 651 bytes
[6/9/2005 8:04:33 AM] Read Some Bytes
[6/9/2005 8:04:33 AM] Read 0 Bytes
[6/9/2005 8:04:33 AM] Second CLOSING
[6/9/2005 8:04:33 AM] Third CLOSING
[6/9/2005 8:04:34 AM] Decrypting Error: Length of the data to decrypt
is invalid. | TransformFinalB lock |
System.Security .Cryptography.C ryptographicExc eption: Length of the
data to decrypt is invalid.
at
System.Security .Cryptography.C ryptoAPITransfo rm.TransformFin alBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security .Cryptography.C ryptoStream.Flu shFinalBlock()
at System.Security .Cryptography.C ryptoStream.Clo se()
at FreeAndClear.Fr eeAndClear.Cryp tFile(String password, String
in_file, String out_file, Boolean encrypt)
at FreeAndClear.Fr eeAndClear.Proc essFile(String file_name)
[6/9/2005 8:04:34 AM] Decrypting Error: REMOVING INVALID FILE!
[6/9/2005 8:04:34 AM] MANUALLY DOING MY THING!
[6/9/2005 8:04:34 AM] Decrypt clean up error: Length of the data to
decrypt is invalid.
--------------------------------------------------------------------------------
Jul 21 '05 #1
8 3023
MattP <ma***@intervie wusa.com> wrote:
Ok, with the help of some examples found on the web and some minor
modifications on our own, we have a simple and working encrypt and
decrypt solution. It runs as a service, watches for files with a
specific extension in a specific directory. The files are uploaded by
FTP to this directory. The service then does the following steps:
<snip>
I've included some code below, which is a partial code listing along
with lots of calls to UpdateStatus (which is just updated a log file
with the time and the message) as I was trying to track down this
problem.

Any help anyone could offer would be GREAT.


Rather than manually catching the exception and closing, use a
try/finally block, one for each stream (this would be easy to do in C#,
or the next version of VB.NET - slightly harder here). That way you'll
make sure that you close each of the streams. As it is, I suspect
you're only closing the cryptostream (and failing to do that, due to
the exception) each time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
In the log file, it shows each one getting called. Including the
global one I made for CryptoStream. The close seems to go through,
though it doesn't work, apparently.

And Finally does exist in VB.net... I'll go ahead and move things
around just to see if finally does something different for us.

Thanks much,

M

On Thu, 9 Jun 2005 20:16:33 +0100, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
MattP <ma***@intervie wusa.com> wrote:
Ok, with the help of some examples found on the web and some minor
modifications on our own, we have a simple and working encrypt and
decrypt solution. It runs as a service, watches for files with a
specific extension in a specific directory. The files are uploaded by
FTP to this directory. The service then does the following steps:


<snip>
I've included some code below, which is a partial code listing along
with lots of calls to UpdateStatus (which is just updated a log file
with the time and the message) as I was trying to track down this
problem.

Any help anyone could offer would be GREAT.


Rather than manually catching the exception and closing, use a
try/finally block, one for each stream (this would be easy to do in C#,
or the next version of VB.NET - slightly harder here). That way you'll
make sure that you close each of the streams. As it is, I suspect
you're only closing the cryptostream (and failing to do that, due to
the exception) each time.


Jul 21 '05 #3
MattP <ma***@intervie wusa.com> wrote:
In the log file, it shows each one getting called. Including the
global one I made for CryptoStream. The close seems to go through,
though it doesn't work, apparently.
It shows CryptoStream.Cl ose being called, but not Close on the other
streams. That's the problem, I believe. I believe CryptoStream.Cl ose is
throwing an exception each time, stopping the other Closes from being
called.
And Finally does exist in VB.net...


Yes, but the Using statement which would make it much easier doesn't.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Jon, you correct, it's throwing this exception when I try to close the
cryptostream:

Value cannot be null.
Parameter name: inputBuffer | TransformFinalB lock |
System.Argument NullException: Value cannot be null.
Parameter name: inputBuffer

I have no idea to get around that.

M

On Thu, 9 Jun 2005 20:41:58 +0100, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
MattP <ma***@intervie wusa.com> wrote:
In the log file, it shows each one getting called. Including the
global one I made for CryptoStream. The close seems to go through,
though it doesn't work, apparently.


It shows CryptoStream.Cl ose being called, but not Close on the other
streams. That's the problem, I believe. I believe CryptoStream.Cl ose is
throwing an exception each time, stopping the other Closes from being
called.
And Finally does exist in VB.net...


Yes, but the Using statement which would make it much easier doesn't.


Jul 21 '05 #5
MattP <ma***@intervie wusa.com> wrote:
Jon, you correct, it's throwing this exception when I try to close the
cryptostream:

Value cannot be null.
Parameter name: inputBuffer | TransformFinalB lock |
System.Argument NullException: Value cannot be null.
Parameter name: inputBuffer

I have no idea to get around that.


Well, that's probably when you try to close it the second time. Just
try to close it once, but make sure you close *all* the streams. That's
where Try/Finally comes in - have three nested Try blocks, each with a
Finally block that closes one of the streams.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
I'll give that a try... but shouldn't checking to see if the object
is nothing work after I've set it to nothing?

M

On Thu, 9 Jun 2005 21:32:28 +0100, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
MattP <ma***@intervie wusa.com> wrote:
Jon, you correct, it's throwing this exception when I try to close the
cryptostream:

Value cannot be null.
Parameter name: inputBuffer | TransformFinalB lock |
System.Argument NullException: Value cannot be null.
Parameter name: inputBuffer

I have no idea to get around that.


Well, that's probably when you try to close it the second time. Just
try to close it once, but make sure you close *all* the streams. That's
where Try/Finally comes in - have three nested Try blocks, each with a
Finally block that closes one of the streams.


Jul 21 '05 #7
MattP <ma***@intervie wusa.com> wrote:
I'll give that a try... but shouldn't checking to see if the object
is nothing work after I've set it to nothing?


No, because you're not getting as far as setting the variable to
nothing (note the terminology here - you can't set an object to
nothing, it doesn't make sense as a concept). The CryptoStream.Cl ose
call is throwing the exception, so you're getting out to the next catch
block.

(It's very rarely worth setting things to null/Nothing explicitly in my
experience, by the way. It just makes the code harder to read.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
Jon,

You are the winner! It was doing exactly as you thought and I just
wasn't following it correctly. Got it fixed and all is well now.

Thanks a lot of the help.

As for setting "variables" to nothing, I agree completely, I was just
getting desperate. :D

M

On Thu, 9 Jun 2005 21:43:40 +0100, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
MattP <ma***@intervie wusa.com> wrote:
I'll give that a try... but shouldn't checking to see if the object
is nothing work after I've set it to nothing?


No, because you're not getting as far as setting the variable to
nothing (note the terminology here - you can't set an object to
nothing, it doesn't make sense as a concept). The CryptoStream.Cl ose
call is throwing the exception, so you're getting out to the next catch
block.

(It's very rarely worth setting things to null/Nothing explicitly in my
experience, by the way. It just makes the code harder to read.)


Jul 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12927
by: weixiang | last post by:
Hi, I want to use DES and CryptoStream to serialize a encrypted stream to a file with a header "CRYPT". And I wrote these code: To store: FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); byte signture = AE.GetBytes("CRYPT");
1
1710
by: Saper\(ek\) | last post by:
I need to encrypt some data in my program, so I've created 2 functions to encrypt and decrypt data. I've created a simple program to test it, and... it crashes. It wors ok on XP, but on win98 it generates an exception (padding is invalid and cannot be removed) When I change coding of chars from Unicode, to utf8 or 7, it does not work even under XP. what can I do, for it to work on XP and win98?? this is the program
10
2044
by: Alejandro Castañaza | last post by:
Hi. I'm writing a program, and I need to send confidential data through the network, so I decided to use encryption, using the System.Security.Cryptography namespace. I'm using the sockets for the network communications, and the program first does a key exchange, with the asymetric cipher classes, to get a new key for the symmetric cipher. My problem is, that although I have checked that the two points get to the same key and...
8
318
by: MattP | last post by:
Ok, with the help of some examples found on the web and some minor modifications on our own, we have a simple and working encrypt and decrypt solution. It runs as a service, watches for files with a specific extension in a specific directory. The files are uploaded by FTP to this directory. The service then does the following steps: 1) Verify it can open the file (so we know it's fully uploaded). 2) Try to decrypt the file with known...
1
1472
by: Simon Whale | last post by:
Hi All, i am using the following two functions to encrypt data, bank details. i can encrypt the details no problems. but when i try to decrypt the details i get the following error "PKCS7 padding is invalid and cannot be removed" does anybody know i can encrypt and decrypt data from a database ? any pointers would be greatfully appreciated!!
3
2131
by: pachinco | last post by:
Hello, I am having a problem encrypting a tiff image..it always loses information after i decrypt and I know it has something to do with the encoding but i can't figure it out. Any help would be appreciated.. Here is the encrypt function: private byte Encrypt(byte bytes) { key = new byte; iv = new byte;
7
13382
by: semedao | last post by:
Hi, I am using cryptostream on both sides on tcp connection that pass data. I am also use asyc socket , so , the data that recieved in the callback method not always have the length of the buffer I sent. for ex I want to send 10000 bytes I can get it in 10 times of 1000 bytes each. so , I need to know when I complete the receiving , I want to write inside cryptostream and check the position compare it to the length I already know I...
9
1958
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely raising exceptions and I've noticed that if an exception of "Length of the data to decrypt is invalid." is raised with the CryptoStream object, later this exception will get raised a second time and thrown to the caller when trying to close the stream...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.