Error reading decrypted stream using AesCryptoServiceProvider

groadsvb

Well-known member
Joined
Nov 13, 2006
Messages
75
Programming Experience
Beginner
The code below was taken from msdn site. The Encryption code that I am using seems to work at this time but the decryption does not. I get an error on the line srDecrypt.ReadToEnd(). I get the error "Value cannot be null. Parameter name:inputbuffer". I have walked it the that line and I am not sure what the error means or how to determine what the error means. If you can tell me what the error is that will be great. Also if anyone has guidance on how to determine the cause of such errrors, that would be fantastic also. Thanks.


VB.NET:
  Shared Function DecryptStringFromBytes_Aes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
        ' Check arguments. 
        If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
            Throw New ArgumentNullException("cipherText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        ' Declare the string used to hold 
        ' the decrypted text. 
        Dim plaintext As String = Nothing

        ' Create an AesCryptoServiceProvider object 
        ' with the specified key and IV. 
        Using aesAlg As New AesCryptoServiceProvider()

            aesAlg.Key = Key
            aesAlg.IV = IV

            ' Create a decrytor to perform the stream transform. 
            Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

            ' Create the streams used for decryption. 
            Using msDecrypt As New MemoryStream(cipherText)

                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

                    Using srDecrypt As New StreamReader(csDecrypt)

                        ' Read the decrypted bytes from the decrypting stream 
                        ' and place them in a string.
                        plaintext = srDecrypt.ReadToEnd()
                    End Using
                End Using
            End Using
        End Using
        Return plaintext

    End Function 'DecryptStringFromBytes_Aes
 
I found the problem with my application. The data that was being passed into it was not complete and had a zero in the first element of the array. I was using a tcpCLient to pass the data over to the app that would decrypt it.
 

Latest posts

Back
Top