CryptographicException: Length of the data to decrypt is invalid

Discussion in 'ASP .Net Security' started by Barb, Apr 11, 2006.

  1. Barb

    Barb Guest

    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
     
    Barb, Apr 11, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hannibal111111

    Length of the data to decrypt is invalid

    Hannibal111111, Jun 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    7,270
    Hannibal111111
    Jun 27, 2006
  2. Brent K
    Replies:
    2
    Views:
    674
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jul 12, 2007
  3. hivie

    Length of data to decrypt is invalid Rijndael

    hivie, Jun 13, 2005, in forum: ASP .Net Security
    Replies:
    2
    Views:
    554
    Duane Laflotte
    Jun 13, 2005
  4. Bishoy George

    Length of the data to decrypt is invalid

    Bishoy George, Apr 5, 2006, in forum: ASP .Net Security
    Replies:
    4
    Views:
    681
    Bishoy George
    Apr 7, 2006
  5. Replies:
    1
    Views:
    460
    Daniel Martin
    Jun 16, 2007
Loading...

Share This Page