2015-06-17 24 views
0

我在試圖打開它時遇到了這個不良數據問題。任何想法如何解決它?當我調試它時表明CryptoStream.FlushFinalBlock()有問題。看到下面的代碼。System.Security.Cryptography.CryptographicException:Bad Data

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 

回答

0

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

怎麼樣我的解密代碼? –

+0

呢?它不工作? –

+0

無法正常工作...>< –

0

公共功能解密(BYVAL inputInBytes()作爲字節)As String 'UTFEncoding用於轉換解密特德字節陣列 '信息返回到一個字符串。 昏暗utf8encoder作爲UTF8Encoding =新的UTF8Encoding() 昏暗tdesProvider作爲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 

末級

相關問題