Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何处理加密异常:“要解密的数据长度无效”?_Vb.net_Cryptography - Fatal编程技术网

Vb.net 如何处理加密异常:“要解密的数据长度无效”?

Vb.net 如何处理加密异常:“要解密的数据长度无效”?,vb.net,cryptography,Vb.net,Cryptography,我正在与docs.micrsoft.com上的一起编码。代码如下: Imports System.Security.Cryptography Public NotInheritable Class Simple3Des Private TripleDes As New TripleDESCryptoServiceProvider Sub New(ByVal key As String) ' Initialize the crypto provider.

我正在与docs.micrsoft.com上的一起编码。代码如下:

Imports System.Security.Cryptography

Public NotInheritable Class Simple3Des

    Private TripleDes As New TripleDESCryptoServiceProvider


    Sub New(ByVal key As String)
        ' Initialize the crypto provider.
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
    End Sub


    Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()

        Dim sha1 As New SHA1CryptoServiceProvider

        ' Hash the key.
        Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ' Truncate or pad the hash.
        ReDim Preserve hash(length - 1)
        Return hash

    End Function


    Public Function EncryptData(ByVal plaintext As String) As String

        ' Convert the plaintext string to a byte array.
        Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)

        ' Create the stream.
        Dim ms As New System.IO.MemoryStream

        ' Create the encoder to write to the stream.
        Dim encStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
        encStream.FlushFinalBlock()

        ' Convert the encrypted stream to a printable string.
        Return Convert.ToBase64String(ms.ToArray)

    End Function


    Public Function DecryptData(ByVal encryptedtext As String) As String

        ' Convert the encrypted text string to a byte array.
        Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

        ' Create the stream.
        Dim ms As New System.IO.MemoryStream
        ' Create the decoder to write to the stream.
        Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
        decStream.FlushFinalBlock()

        ' Convert the plaintext stream to a string.
        Return System.Text.Encoding.Unicode.GetString(ms.ToArray)

    End Function

End Class
运行应用程序时,我收到一个错误:

类型的未处理异常

mscorlib.dll中出现“System.Security.Cryptography.CryptographyException”

附加信息:要解密的数据长度无效

该错误发生在行ReDim PREVICE hashlength-1上的TruncateHash中


我已经在SO和其他论坛上搜索了这个错误。但是,我在将我找到的修复应用于此代码时遇到了问题。

您在EncryptData函数中有一个复制/粘贴错误。您应该调用TrippleDes.CreateEncryptor而不是TrippleDes.CreateDecryptor

解密程序需要对齐的加密数据块,这会导致异常

您的错误发生在这一行

encStream.FlushFinalBlock()
而不是宣布的

ReDim Preserve hash(length - 1)