Help with CryptoStream and incomplete files...

M

MattP

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. | TransformFinalBlock |
System.Security.Cryptography.CryptographicException: Length of the
data to decrypt is invalid.
at
System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Close()
------

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.Close, CryptoStream.Clear, 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(ByVal file_name As String) As Boolean
Dim tempPath As String
Dim tempName As String
Dim testStream As FileStream

'Update Status
UpdateStatus("Processing: " & file_name)

'Try opening if the file, if it fails, start the timer.
Try
testStream = New FileStream(file_name, FileMode.Open,
FileAccess.Read)
testStream.Close()
Catch ex As System.IO.IOException
UpdateStatus("Unable to Open File: (expected error): " &
ex.Message)
timeRetry.Enabled = True
Return False
Catch ex As Exception
UpdateStatus("Unexpected Error Occured: " & ex.Message)
timeRetry.Enabled = True
Return False
End Try

'Decrypt File
UpdateStatus("Decrypting: " & file_name)
tempPath = file_name.Substring(0, file_name.LastIndexOf("."))
& ".wmv"
tempName = tempPath.Substring(tempPath.LastIndexOf("\") + 1,
tempPath.Length - (tempPath.LastIndexOf("\") + 1))
Try
CryptFile(m_Password, file_name, tempPath, False)
Catch ex As Exception
UpdateStatus("Decrypting 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("Decrypting Error: REMOVING INVALID
FILE!")
If Not crypto_stream Is Nothing Then
UpdateStatus("MANUALLY DOING MY THING!")
crypto_stream.Close()
crypto_stream = Nothing
End If
System.IO.File.Delete(file_name)
System.IO.File.Delete(tempPath)
Catch ex1 As Exception
UpdateStatus("Decrypt 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 TripleDESCryptoServiceProvider
Dim block_size_bits As Integer
Dim key As Byte() = Nothing
Dim iv As Byte() = Nothing
Dim crypto_transform As ICryptoTransform
Const BLOCK_SIZE As Integer = 1024
Dim buffer(BLOCK_SIZE) As Byte
Dim bytes_read As Integer


UpdateStatus("CRYPTFILE!")
Try
in_stream = New FileStream(in_file, FileMode.Open,
FileAccess.Read)
out_stream = New FileStream(out_file, FileMode.Create,
FileAccess.Write)
Catch ex As Exception
UpdateStatus("First 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.Close()
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.ValidKeySize(i) Then
key_size_bits = i
Exit For
End If
Next i
UpdateStatus("Key Size: " & key_size_bits)
Debug.Assert(key_size_bits > 0)

' Get the block size for this provider.
UpdateStatus("Block Size: " & block_size_bits)
block_size_bits = des_provider.BlockSize

' Generate the key and initialization vector.
UpdateStatus("Make Key and IV!")
MakeKeyAndIV(password, key_size_bits, block_size_bits,
key, iv)

UpdateStatus("Setting to Decrypt")
' Make the encryptor or decryptor.
If encrypt Then
crypto_transform = des_provider.CreateEncryptor(key,
iv)
Else
crypto_transform = des_provider.CreateDecryptor(key,
iv)
End If

' Attach a crypto stream to the output stream.
UpdateStatus("Attaching the crypto stream")
crypto_stream = New CryptoStream(out_stream,
crypto_transform, CryptoStreamMode.Write)

UpdateStatus("At the Do loop")
Do
Try
' Read some bytes.
UpdateStatus("Read Some Bytes")
bytes_read = in_stream.Read(buffer, 0, BLOCK_SIZE)
If bytes_read = 0 Then
UpdateStatus("Read 0 Bytes")
If Not crypto_stream Is Nothing Then
crypto_stream.Close()
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.Close()
out_stream = Nothing
End If
Exit Do
End If

' Write the bytes into the CryptoStream.
UpdateStatus("Encrypting " & bytes_read & "
bytes")
crypto_stream.Write(buffer, 0, bytes_read)
Catch ex As Exception
' Close the streams.
UpdateStatus("Second CLOSING")
If Not crypto_stream Is Nothing Then
crypto_stream.Close()
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.Close()
out_stream = Nothing
End If

Throw ex
End Try
Loop
Catch ex As Exception
' Close the streams.
UpdateStatus("Third CLOSING")
If Not crypto_stream Is Nothing Then
crypto_stream.Close()
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.Close()
out_stream = Nothing
End If

Throw ex
End Try

' Close the streams.
UpdateStatus("Last CLOSING")
crypto_stream.Close()
in_stream.Close()
out_stream.Close()
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\wwwroot\Vivid\video\33291394.xxx
[6/9/2005 8:04:32 AM] Decrypting:
C:\Inetpub\wwwroot\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. | TransformFinalBlock |
System.Security.Cryptography.CryptographicException: Length of the
data to decrypt is invalid.
at
System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Close()
at FreeAndClear.FreeAndClear.CryptFile(String password, String
in_file, String out_file, Boolean encrypt)
at FreeAndClear.FreeAndClear.ProcessFile(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.
--------------------------------------------------------------------------------
 
J

Jon Skeet [C# MVP]

MattP said:
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:

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.
 
M

MattP

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
 
J

Jon Skeet [C# MVP]

MattP said:
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.Close being called, but not Close on the other
streams. That's the problem, I believe. I believe CryptoStream.Close 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.
 
M

MattP

Jon, you correct, it's throwing this exception when I try to close the
cryptostream:

Value cannot be null.
Parameter name: inputBuffer | TransformFinalBlock |
System.ArgumentNullException: Value cannot be null.
Parameter name: inputBuffer

I have no idea to get around that.

M
 
J

Jon Skeet [C# MVP]

MattP said:
Jon, you correct, it's throwing this exception when I try to close the
cryptostream:

Value cannot be null.
Parameter name: inputBuffer | TransformFinalBlock |
System.ArgumentNullException: 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.
 
M

MattP

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
 
J

Jon Skeet [C# MVP]

MattP said:
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.Close
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.)
 
M

MattP

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top