Rijndael 解密错误 - 要解密的数据长度无效
我可以使用 rijndeal 加密 zip 文件,但是当我解密时,我收到一条错误,提示“要解密的数据长度无效”,我正在从文件中获取要解密的字节数组。这是我获取字节数组的方法。
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)
一旦我将字节数组传递到 Decrypt 方法中,当我尝试使用加密流读取时,就会收到错误。
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("S@ltVa|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("S@ltVa|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
I am able to encrypt a zip file using rijndeal but when i decrypt I get an error that says "Length of the data to decrypt is invalid" Im getting the byte array to decrypt from a file. Here is how i get the byte array.
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)
Once i pass the byte array into the Decrypt method I get the error when I try to read with the 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("S@ltVa|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
Any ideas what im doing wrong?
Also here is the code they encrypts:
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("S@ltVa|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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将文件流代码替换为 System.IO.File.ReadAllBytes、System.IO.File.WriteAllBytes。如果这有效,那么您就知道文件流导致了问题。
Replace your filestream code with System.IO.File.ReadAllBytes, System.IO.File.WriteAllBytes. If this works then you know filestream is causing the issue.
您如何保存加密数据?您是使用纯文本(即 ASCII、UTF-8 等)对其进行编码,还是使用 Base-64 之类的内容对其进行编码?尝试将其加密为字节数组,然后立即解密。如果它有效并且解密成功,则说明存在编码问题。
How are you saving the encrypted data? Are you encoding it using plain text (i.e. ASCIi, UTF-8, etc) or are you encoding it with something like Base-64? Try encrypting it to a byte array, then immediately decrypting it. If it works and it decrypts successfully, you have an encoding problem.
这修复了它:
This fixed it: