VB.NET要解密的数据长度无效

时间:2014-01-27 03:14:50

标签: vb.net encryption aes

我看了其他答案,但我仍感到困惑。

我正在测试我在ASP.NET项目中复制的加密代码。我不确定为什么它在vb.net中有错误。帮助!

提前致谢

  Dim EncryptionKey As String = "SamplePassword"

    Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() { 8 bytes here}, 10000)
    Using fileCrypt As New FileStream(Application.StartupPath + "\output.txt", FileMode.Create)
        Using encrypt As New AesManaged()
            Using cs As New CryptoStream(fileCrypt, encrypt.CreateDecryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Write)
                Using fileInput As New FileStream(Application.StartupPath + "\input.txt", FileMode.Open)
                    encrypt.KeySize = 256
                    encrypt.BlockSize = 128
                    Dim data As Integer
                    While (InlineAssignHelper(data, fileInput.ReadByte())) <> -1
                        cs.WriteByte(CByte(data))

                    End While
                End Using

            End Using
        End Using

    End Using

1 个答案:

答案 0 :(得分:0)

试试这个:

  Dim plaintext as String
  Dim EncryptionKey As String = "SamplePassword"
  Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() { 8 bytes here}, 10000)

  Using encrypt As New AesManaged
        encrypt.Key = pdb.GetBytes(32)
        encrypt.IV = pdb.GetBytes(16)

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

        ' Create the streams used for decryption. 
        Using msDecrypt As New MemoryStream(File.ReadAllBytes(Application.StartupPath + "\input.txt"))
            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