CryptographicException: Length of the data to decrypt is invalid

B

Barb

I've got a decryption function that works on files. It's a .NET COM-enabled
object, and it works most everywhere except on this one server. The strange
part is that when my decryption logic runs, it actually decrypts some of the
file, and then doesn't keep going. I can successfully decrypt the same file
in a different environment with no problems. So, it might decrypt the 1st
half of the first line of the file, and then leave the rest decrypted (and my
original file gets corrupted somehow).

Anyway, I'm hoping somebody can take a look at the following routine that
uses 3Des and see if there's something different I can be doing to make sure
that the encrypted file decrypts properly. I'm thinking that in this
environment, maybe I'm hitting a column delimiter, or some odd character.

Any suggestions are appreciated:

Public Function DecryptFile(ByVal SourcePath As String, ByVal decryptPath As
String) As String
Dim stream1 As FileStream = Nothing
Dim stream2 As FileStream = Nothing
Try
Dim buffer1 As Byte() = New Byte(128 - 1) {}
Dim info1 As New FileInfo(SourcePath)
Dim info2 As New FileInfo(decryptPath)
If info2.Exists Then
info2.Delete()
End If
stream1 = New FileStream(info1.FullName, FileMode.Open)
stream2 = New FileStream(decryptPath, FileMode.CreateNew)
Dim stream3 As New CryptoStream(stream1, Me.des.CreateDecryptor,
CryptoStreamMode.Read)
Dim num1 As Integer = stream3.Read(buffer1, 0, buffer1.Length)
Do While (num1 > 0)
stream2.Write(buffer1, 0, num1)
stream2.Flush()
num1 = stream3.Read(buffer1, 0, buffer1.Length)
Loop
stream2.Close()
stream1.Close()
Catch exception1 As UnauthorizedAccessException
Return ("Make sure the permissions are set correctly for the
user accessing. " & exception1.Message)
Catch exception2 As Exception
Return exception2.ToString
Finally
If (Not stream2 Is Nothing) Then
stream2.Close()
End If
If (Not stream1 Is Nothing) Then
stream1.Close()
End If
End Try
Return "Decrypt Complete"
End Function
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top