填充无效RijndaelManaged

时间:2014-05-11 14:12:30

标签: vb.net encryption

我收到错误填充无效,无法在pass = srDecrypt.ReadToEnd()的以下.net类中删除。请帮忙。

 Friend NotInheritable Class Crypto
        Private Shared _salt As Byte()

        Shared Sub New()
            Crypto._salt = Encoding.ASCII.GetBytes("o6B06642kbM7c5")
        End Sub

        Public Sub New()
        End Sub

        Public Shared Function Decrypt(ByVal cipherText As String, ByVal unknown As String) As String
            If String.IsNullOrEmpty(cipherText) Then
                Throw New ArgumentNullException("cipherText")
            End If
            If String.IsNullOrEmpty(unknown) Then
                Throw New ArgumentNullException("unknown")
            End If
            Dim rijndaelManaged As RijndaelManaged = Nothing
            Dim pass As String = Nothing
            Try
                Dim rfc2898DeriveByte As New Rfc2898DeriveBytes(unknown, Crypto._salt)
                Using memoryStream As New MemoryStream(Convert.FromBase64String(cipherText))
                    rijndaelManaged = New RijndaelManaged()
                    Using rijndaelManaged
                        rijndaelManaged.Key = rfc2898DeriveByte.GetBytes(rijndaelManaged.KeySize \ 8)
                        rijndaelManaged.IV = Crypto.ReadByteArray(memoryStream)
                    End Using

                    Dim decryptor As ICryptoTransform = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV)
                    Using csDecrypt As New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)
                        Using srDecrypt As New StreamReader(csDecrypt)
                            pass = srDecrypt.ReadToEnd()
                        End Using
                    End Using
                End Using
            Finally
                If rijndaelManaged IsNot Nothing Then
                    rijndaelManaged.Clear()
                End If
            End Try
            Return pass
        End Function

        Private Shared Function ReadByteArray(ByVal s As Stream) As Byte()
            Dim numArray As Byte() = New Byte(3) {}
            If s.Read(numArray, 0, CInt(numArray.Length)) <> CInt(numArray.Length) Then
                Throw New SystemException("Stream did not contain properly formatted byte array")
            End If
            Dim numArray1 As Byte() = New Byte(BitConverter.ToInt32(numArray, 0) - 1) {}
            If s.Read(numArray1, 0, CInt(numArray1.Length)) <> CInt(numArray1.Length) Then
                Throw New SystemException("Did not read byte array properly")
            End If
            Return numArray1
        End Function
    End Class

0 个答案:

没有答案