填充无效,无法删除。填充零有效,但 ANSIX923、ISO10126 或 PKCS7 无效
Padding is invalid and cannot be removed. Padding Zeros works but ANSIX923, ISO10126 or PKCS7 do not
编辑:Here is a .NET fiddle of the issue
我目前正在尝试编写一个进程来加密以字节流形式发送的文件。 AES 库似乎没有正确应用填充。
当我尝试进行加密时,我得到这样的密码字节:
... 50 57 243 226 18 0 0 0 0 0 0 0 0 0 ... (about 30,000 0s appended to the end)
当我认为我应该得到这样的密码字节时(使用 PKCS7):
... 50 57 243 226 18 9 9 9 9 9 9 9 9 9
如何让实际的填充方法被识别并像 PKCS7 那样 return 填充?
Public Function Encrypt(InputStream As Stream) As Byte()
Dim outputStream = New MemoryStream()
Dim EncryptionKey As String = "MAKV2SPBNI99212"
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using cs As New CryptoStream(outputStream, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
Dim data As Integer
While (Assign(data, InputStream.ReadByte())) <> -1
cs.WriteByte(CByte(data))
End While
End Using
End Using
Return outputStream.GetBuffer()
End Function
Public Function Decrypt(InputStream As Stream) As Byte()
Dim outputStream = New MemoryStream()
Dim EncryptionKey As String = "MAKV2SPBNI99212"
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using cs As New CryptoStream(InputStream, encryptor.CreateDecryptor(), CryptoStreamMode.Read)
Dim data As Integer
While (Assign(data, cs.ReadByte())) <> -1
outputStream.WriteByte(CByte(data))
End While
End Using
End Using
Return outputStream.GetBuffer
End Function
Private Function Assign(Of T)(ByRef source As T, ByVal value As T) As T
source = value
Return value
End Function
您不应在 returned MemoryStreams 上使用 .GetBuffer
方法。 .GetBuffer
returns 底层数组,通常大于流中的数据量。这就是为什么您在末尾看到额外的 0,以及填充无法正确读取的原因。请参阅 the documentation for the MemoryStream.GetBuffer Method
中的备注部分
您应该做的是调用 outputStream.ToArray
,这将 return 一个仅包含流中包含的相关数据的字节数组,没有所有尾随 0。
编辑:Here is a .NET fiddle of the issue
我目前正在尝试编写一个进程来加密以字节流形式发送的文件。 AES 库似乎没有正确应用填充。
当我尝试进行加密时,我得到这样的密码字节:
... 50 57 243 226 18 0 0 0 0 0 0 0 0 0 ... (about 30,000 0s appended to the end)
当我认为我应该得到这样的密码字节时(使用 PKCS7):
... 50 57 243 226 18 9 9 9 9 9 9 9 9 9
如何让实际的填充方法被识别并像 PKCS7 那样 return 填充?
Public Function Encrypt(InputStream As Stream) As Byte()
Dim outputStream = New MemoryStream()
Dim EncryptionKey As String = "MAKV2SPBNI99212"
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using cs As New CryptoStream(outputStream, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
Dim data As Integer
While (Assign(data, InputStream.ReadByte())) <> -1
cs.WriteByte(CByte(data))
End While
End Using
End Using
Return outputStream.GetBuffer()
End Function
Public Function Decrypt(InputStream As Stream) As Byte()
Dim outputStream = New MemoryStream()
Dim EncryptionKey As String = "MAKV2SPBNI99212"
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using cs As New CryptoStream(InputStream, encryptor.CreateDecryptor(), CryptoStreamMode.Read)
Dim data As Integer
While (Assign(data, cs.ReadByte())) <> -1
outputStream.WriteByte(CByte(data))
End While
End Using
End Using
Return outputStream.GetBuffer
End Function
Private Function Assign(Of T)(ByRef source As T, ByVal value As T) As T
source = value
Return value
End Function
您不应在 returned MemoryStreams 上使用 .GetBuffer
方法。 .GetBuffer
returns 底层数组,通常大于流中的数据量。这就是为什么您在末尾看到额外的 0,以及填充无法正确读取的原因。请参阅 the documentation for the MemoryStream.GetBuffer Method
您应该做的是调用 outputStream.ToArray
,这将 return 一个仅包含流中包含的相关数据的字节数组,没有所有尾随 0。