2014-01-27 38 views
0

我看了其他答案,但我仍然感到困惑。VB.NET要解密的数据长度无效

我正在测试我在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 

回答

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 
+0

似乎无法使用你的答案。我正在使用其他流进行加密 –

+0

已更改答案。不知道这是否适合你。 – Sameer

+0

伤心它不起作用:( –

相关问题