AES 解密适用于 python 但不适用于 .NET
AES decryption works in python but not in .NET
此python代码完美运行并解密密文:
import sys, hexdump, binascii
from Crypto.Cipher import AES
class AESCipher:
def __init__(self, key):
self.key = key
def decrypt(self, iv, data):
self.cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self.cipher.decrypt(data)
key = binascii.unhexlify("0602000000a400005253413100040000")
iv = binascii.unhexlify("0100010067244F436E6762F25EA8D704")
hex_str_cipher = "d690a9d0a592327f99bb4c6a6b6d4cbe"
ciphertext = binascii.unhexlify(hex_str_cipher)
raw_un = AESCipher(key).decrypt(iv, ciphertext)
password = raw_un.decode('utf-16')
print(password)
但尝试在 VB.NET 中重新创建它,如下所示:
Sub Main()
Dim AesEnc = System.Security.Cryptography.Aes.Create()
AesEnc.Mode = CipherMode.CBC
AesEnc.KeySize = 128
AesEnc.Key = HexToBytes("0602000000a400005253413100040000")
AesEnc.IV = HexToBytes("0100010067244F436E6762F25EA8D704")
Dim EncryptedBytes As Byte() = HexToBytes("d690a9d0a592327f99bb4c6a6b6d4cbe")
Using MemStrm As New IO.MemoryStream(EncryptedBytes)
Using CryptStrm As New CryptoStream(MemStrm, AesEnc.CreateDecryptor(), CryptoStreamMode.Read)
Dim PlaintextBytes(EncryptedBytes.Length - 1) As Byte
CryptStrm.Read(PlaintextBytes, 0, PlaintextBytes.Length)
Console.WriteLine(System.Text.Encoding.Unicode.GetString(PlaintextBytes))
End Using
End Using
End Sub
Public Function HexToBytes(HexString As String) As Byte()
Dim ByteLength = HexString.Length \ 2
Dim Bytes(ByteLength - 1) As Byte
For i = 0 To ByteLength - 1
Bytes(i) = Convert.ToByte(HexString.Substring(i * 2, 2), 16)
Next
Return Bytes
End Function
调用 CryptStrm.Read
总是失败,错误为 "Padding is invalid and cannot be removed"
在使用上述例程之前,我已经在 .NET 中多次执行 AES encryption/decryption,所以我不明白为什么会失败,就好像 key/IV 与python 脚本正在使用什么。有任何想法吗?
感谢@OguzOzgul 对填充的评论,我尝试将这一行添加到我的 .NET 代码中,现在它运行良好
AesEnc.Padding = PaddingMode.Zeros
此python代码完美运行并解密密文:
import sys, hexdump, binascii
from Crypto.Cipher import AES
class AESCipher:
def __init__(self, key):
self.key = key
def decrypt(self, iv, data):
self.cipher = AES.new(self.key, AES.MODE_CBC, iv)
return self.cipher.decrypt(data)
key = binascii.unhexlify("0602000000a400005253413100040000")
iv = binascii.unhexlify("0100010067244F436E6762F25EA8D704")
hex_str_cipher = "d690a9d0a592327f99bb4c6a6b6d4cbe"
ciphertext = binascii.unhexlify(hex_str_cipher)
raw_un = AESCipher(key).decrypt(iv, ciphertext)
password = raw_un.decode('utf-16')
print(password)
但尝试在 VB.NET 中重新创建它,如下所示:
Sub Main()
Dim AesEnc = System.Security.Cryptography.Aes.Create()
AesEnc.Mode = CipherMode.CBC
AesEnc.KeySize = 128
AesEnc.Key = HexToBytes("0602000000a400005253413100040000")
AesEnc.IV = HexToBytes("0100010067244F436E6762F25EA8D704")
Dim EncryptedBytes As Byte() = HexToBytes("d690a9d0a592327f99bb4c6a6b6d4cbe")
Using MemStrm As New IO.MemoryStream(EncryptedBytes)
Using CryptStrm As New CryptoStream(MemStrm, AesEnc.CreateDecryptor(), CryptoStreamMode.Read)
Dim PlaintextBytes(EncryptedBytes.Length - 1) As Byte
CryptStrm.Read(PlaintextBytes, 0, PlaintextBytes.Length)
Console.WriteLine(System.Text.Encoding.Unicode.GetString(PlaintextBytes))
End Using
End Using
End Sub
Public Function HexToBytes(HexString As String) As Byte()
Dim ByteLength = HexString.Length \ 2
Dim Bytes(ByteLength - 1) As Byte
For i = 0 To ByteLength - 1
Bytes(i) = Convert.ToByte(HexString.Substring(i * 2, 2), 16)
Next
Return Bytes
End Function
调用 CryptStrm.Read
总是失败,错误为 "Padding is invalid and cannot be removed"
在使用上述例程之前,我已经在 .NET 中多次执行 AES encryption/decryption,所以我不明白为什么会失败,就好像 key/IV 与python 脚本正在使用什么。有任何想法吗?
感谢@OguzOzgul 对填充的评论,我尝试将这一行添加到我的 .NET 代码中,现在它运行良好
AesEnc.Padding = PaddingMode.Zeros