我能夠使用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
你的意思是Rijndael算法?這是正常的名字...... – 2009-09-09 00:11:30
你能加入加密zip文件的代碼嗎? – russau 2009-09-09 00:40:02
是的,我認爲Rijndael – Thomas 2009-09-09 01:29:07