2009-09-09 122 views
1

我能夠使用rijndeal加密zip文件,但是當我解密時出現錯誤,指出「要解密的數據的長度無效」我獲取字節數組從文件解密。這是我如何獲得字節數組。Rijndael解密錯誤 - 要解密的數據的長度無效

Dim FStream As FileStream = File.OpenRead("<Filepath>") 
EncData = New Byte(FStream.Length) {} 
FStream.Read(EncData, 0, EncData.Length) 
Dim DecryptedBytes As Byte() = DataVault.RijndealController.Decrypt(EncData, Password) 

一旦我的字節數組傳遞到解密方法,我得到的錯誤,當我嘗試使用的CryptoStream閱讀。

Public Function Decrypt(ByVal Input As Byte(), ByVal Password As String) As Byte() 

Try 
    Dim PasswordBytes As Byte() = Encoding.UTF8.GetBytes(Password) 
    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes("@0B4c3D4e5Y6r7H2") 
    Dim SaltValue As Byte() = Encoding.UTF8.GetBytes("[email protected]|u<") 


    Dim DerivedBytes As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(PasswordBytes,SaltValue, 4) 
    Dim keyBytes As Byte() = DerivedBytes.GetBytes(32) 

Dim symmetricKey As RijndaelManaged 
symmetricKey = New RijndaelManaged() 
symmetricKey.Mode = CipherMode.CBC 

Dim decryptor As ICryptoTransform 
decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes) 

Dim memoryStream As MemoryStream 
memoryStream = New MemoryStream(Input) 

Dim cryptoStream As CryptoStream 
cryptoStream = New CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read) 

Dim plainTextBytes As Byte() 
ReDim plainTextBytes(Input.Length) 


Dim decryptedByteCount As Integer 
While ((decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)) > 0) 

End While 

memoryStream.Close() 
cryptoStream.Close() 

Return plainTextBytes 

Catch ex As Exception 
Return Nothing 
End Try 

End Function 

任何想法我做錯了什麼?

而且,這裏是他們的加密代碼:

 Public Function EncryptBytes(ByVal Input As Byte(), ByVal Password As String) As Byte() 

     Try 
      Dim PasswordBytes As Byte() = Encoding.UTF8.GetBytes(Password) 
      Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes("@0B4c3D4e5Y6r7H2") 
      Dim SaltValue As Byte() = Encoding.UTF8.GetBytes("[email protected]|u<") 
      Dim InputStringBytes As Byte() = Input 

      Dim DerivedBytes As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(PasswordBytes, SaltValue, 4) 
      Dim keyBytes As Byte() = DerivedBytes.GetBytes(32) 


      Dim symmetricKey As RijndaelManaged 
      symmetricKey = New RijndaelManaged 
      symmetricKey.Mode = CipherMode.CBC 

      Dim encryptor As ICryptoTransform 
      encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes) 
      Dim MStream As New MemoryStream() 

      Dim cryptoStream As CryptoStream 
      cryptoStream = New CryptoStream(MStream, encryptor, CryptoStreamMode.Write) 
      cryptoStream.Write(InputStringBytes, 0, InputStringBytes.Length) 
      cryptoStream.FlushFinalBlock() 

      Dim cipherBytes As Byte() = MStream.ToArray() 
      MStream.Close() 
      cryptoStream.Close() 

      Return cipherBytes 

     Catch ex As Exception 

     End Try 
     Return Encoding.UTF8.GetBytes("0") 
    End Function 
+0

你的意思是Rijndael算法?這是正常的名字...... – 2009-09-09 00:11:30

+0

你能加入加密zip文件的代碼嗎? – russau 2009-09-09 00:40:02

+0

是的,我認爲Rijndael – Thomas 2009-09-09 01:29:07

回答

2

用System.IO.File.ReadAllBytes,System.IO.File.WriteAllBytes替換您的文件流代碼。如果這有效,那麼你知道文件流正在造成這個問題。

+0

這個技巧。一旦我使用Writeallbytes和readallbytes它工作得很好。任何想法爲什麼? – Thomas 2009-09-09 13:06:34

+0

是否有可能在某處傳遞了錯誤的變量長度? – russau 2009-09-09 22:08:48

2

你是如何保存加密的數據?你是使用純文本(即ASCIi,UTF-8等)對它進行編碼還是用Base-64之類的東西對它進行編碼?嘗試將其加密爲一個字節數組,然後立即解密。如果它能正常工作並且解密成功,那麼你就有一個編碼問題。

+0

我把它編碼成一個字節數組,然後用文件流將它寫入一個文件。 – Thomas 2009-09-09 01:31:42

0

這個固定:

Public Function EncryptBytes(ByVal Input As Byte(), ByVal Password As String) As Byte() 

     Try 
      Dim PasswordBytes As Byte() = Encoding.UTF8.GetBytes(Password) 
      Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes("@0B4c3D4e5Y6r7H2") 
      Dim SaltValue As Byte() = Encoding.UTF8.GetBytes("[email protected]|u<") 
      Dim InputStringBytes As Byte() = Input 

      Dim DerivedBytes As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(PasswordBytes, SaltValue, 4) 
      Dim keyBytes As Byte() = DerivedBytes.GetBytes(32) 

      'Generate Rijndael Manager 
      Dim symmetricKey As RijndaelManaged 
      symmetricKey = New RijndaelManaged 
      symmetricKey.Mode = CipherMode.CBC 

      Dim encryptor As ICryptoTransform 
      encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes) 
      Dim MStream As New MemoryStream() 

      Dim cryptoStream As CryptoStream 
      cryptoStream = New CryptoStream(MStream, encryptor, CryptoStreamMode.Write) 
      cryptoStream.Write(InputStringBytes, 0, InputStringBytes.Length) 
      cryptoStream.FlushFinalBlock() 

      Dim cipherBytes As Byte() = MStream.ToArray() 
      MStream.Close() 
      cryptoStream.Close() 

      Return cipherBytes 

     Catch ex As Exception 

     End Try 
     Return Encoding.UTF8.GetBytes("0") 
    End Function 
+1

這可能解決了您的問題,但空的catch塊只能引入新的問題。 – 2012-02-08 17:32:36

相關問題