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