我正在尝试在两个不同的平台上设置 AES 加密/解密。当我加密或解密时,我一直无法让 VB.net 给我预期的输出,所以我做了一个小实验:
使用相同的 System.Security.Cryptography.AesManaged 对象进行加密和解密,我得到了原始数据,但使用不同的对象进行加密和解密,我得到了不同的答案。我的代码如下
Dim AES As New System.Security.Cryptography.AesManaged
Dim AES2 As New System.Security.Cryptography.AesManaged
Dim key() As Byte = New Byte() {&HDE, &HAD, &HBE, &HEF, &HA5, &HF4, &H56, &H12, &HDE, &HAD, &HBA, &HAB, &H1, &H92, &H83, &H74}
Dim nulliv() As Byte = New Byte() {&H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0}
AES.Key = key
AES.KeySize = 128
AES.BlockSize = 128
AES.Padding = Security.Cryptography.PaddingMode.None
AES.Mode = Security.Cryptography.CipherMode.CBC
AES.IV = nulliv
AES2.Key = key
AES2.KeySize = 128
AES2.BlockSize = 128
AES2.Padding = Security.Cryptography.PaddingMode.None
AES2.Mode = Security.Cryptography.CipherMode.CBC
AES2.IV = nulliv
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor()
Dim input() As Byte = New Byte() {&H12, &H34, &H56, &H78, &H9A, &HBC, &HDE, &HF0, &H24, &H68, &HAC, &HE0, &H78, &H94, &H56, &H12}
Dim enc() As Byte = DESEncrypter.TransformFinalBlock(input, 0, input.Length)
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
Dim out() As Byte = DESDecrypter.TransformFinalBlock(enc, 0, enc.Length)
Dim DESDecrypter2 As System.Security.Cryptography.ICryptoTransform = AES2.CreateDecryptor
Dim out2() As Byte = DESDecrypter2.TransformFinalBlock(enc, 0, enc.Length)
我希望输入、out 和 out2 匹配,但它们不匹配。input 和 out 相同,但 out2 不同。我只能假设 AES 对象中存储了一些我没有正确设置或在对象之间正确传输的东西。
我最初尝试 ECB 模式来简化问题,但想知道不设置 IV 是否有效果。所以,我尝试使用 CBC,IV 为零,这意味着它应该与 ECB 相同。在这两种情况下,我都遇到了 out 和 out2 不匹配的相同问题。
谁能向我解释我错过了什么?谢谢