Vb.net System.Security.Cryptography.Cryptography异常:错误数据

Vb.net System.Security.Cryptography.Cryptography异常:错误数据,vb.net,cryptography,Vb.net,Cryptography,当我试图打开它时,我遇到了这个糟糕的数据问题。你知道怎么解决吗?调试时,它显示CryptoStream.FlushFinalBlock()有问题。参见下面的代码 Public Class Encryption Public Function Encrypt(ByVal plainText As String) As Byte() Dim utf8encoder As UTF8Encoding = New UTF8Encoding() Dim inputInBytes() A

当我试图打开它时,我遇到了这个糟糕的数据问题。你知道怎么解决吗?调试时,它显示
CryptoStream.FlushFinalBlock()
有问题。参见下面的代码

Public Class Encryption
  Public Function Encrypt(ByVal plainText As String) As Byte()

    Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)


    Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()

    ' The ICryptTransform interface uses the TripleDES 
    ' crypt provider along with encryption key and init vector 
    ' information 
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)


    Dim encryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)


    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.FlushFinalBlock()
    encryptedStream.Position = 0

    Dim result(encryptedStream.Length - 1) As Byte
    encryptedStream.Read(result, 0, encryptedStream.Length)
    cryptStream.Close()
    Return result
  End Function

  Public Function Decrypt(ByVal inputInBytes() As Byte) As String 
    ' UTFEncoding is used to transform the decrypted Byte Array 
    ' information back into a string. 
    Dim utf8encoder As UTF8Encoding = New UTF8Encoding() 
    Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()

    ' As before we must provide the encryption/decryption key along with 
    ' the init vector. 
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)

    ' Provide a memory stream to decrypt information into 
    Dim decryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.FlushFinalBlock()
    decryptedStream.Position = 0

    ' Read the memory stream and convert it back into a string 
    Dim result(decryptedStream.Length - 1) As Byte
    decryptedStream.Read(result, 0, decryptedStream.Length)
    cryptStream.Close()
    Dim myutf As UTF8Encoding = New UTF8Encoding()
    Return myutf.GetString(result)
  End Function
End Class

cryptStream.Write之后,您可以关闭它并返回
MemoryStream
数据

Public Function Encrypt(ByVal plainText As String) As Byte()

    Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)


    Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()

    ' The ICryptTransform interface uses the TripleDES 
    ' crypt provider along with encryption key and init vector 
    ' information 
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)


    Dim encryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)

    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.Close()
    encryptedStream.Position = 0

    Return encryptedStream.ToArray()
End Function

更新:解密方法

Public Function Decrypt(ByVal inputInBytes() As Byte) As String 
    Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()

    ' As before we must provide the encryption/decryption key along with 
    ' the init vector. 
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)

    ' Provide a memory stream to decrypt information into 
    Dim decryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.FlushFinalBlock()

    Return System.Text.Encoding.Unicode.GetString(decryptedStream.ToArray)
End Function
公共函数解密(ByVal InputBytes()作为字节)作为字符串 'UTFEncoding用于转换解密的字节数组 '将信息返回到字符串中。 将utf8encoder设置为UTF8Encoding=新的UTF8Encoding() Dim tdesProvider As TripleDESCryptoServiceProvider=新的TripleDESCryptoServiceProvider()


结束课堂

你如何编辑你的问题并添加解密代码,这样我就可以看一看?
    ' As before we must provide the encryption/decryption key along with 
    ' the init vector. 
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)

    ' Provide a memory stream to decrypt information into 
    Dim decryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.FlushFinalBlock()
    decryptedStream.Position = 0

    ' Read the memory stream and convert it back into a string 
    Dim result(decryptedStream.Length - 1) As Byte
    decryptedStream.Read(result, 0, decryptedStream.Length)
    cryptStream.Close()
    Dim myutf As UTF8Encoding = New UTF8Encoding()
    Return myutf.GetString(result)
End Function