在 Python 中解密 Kamstrup WMBUS
Decrypt Kamstrup WMBUS in Python
我有一个 Kamstrup WMbus 水表发送这样的帧:
21442D2C529027581B168D2814900939201F0775C6452FBBAC155B46A546035219D51AB8
我正在尝试在 Python 中解密。
打破框架我有以下值:
- 键
16ce383ebc0790e928215525cd4f7abf
- 输入 IV
2D2C529027581B162890093920000000
- 输入数据
1F0775C6452FBBAC155B46A546035219D5
将其粘贴到 http://www.cryptogrium.com/aes-ctr.html 中会生成有效的解密帧:
bbe57934ddc46a71004400000044000000
我已经尝试过 PyCrypto 和 PyCryptodome,但都给出了与 cryptogrium.com 相同的正确答案。
from Cryptodome.Cipher import AES
# ctr = Crypto.Util.Counter.new(128, initial_value=int("0000002039099028161B582790522C2D", 16))
# cipher = Crypto.Cipher.AES.new(ecryptionKey, Crypto.Cipher.AES.MODE_CTR, counter=ctr)
# secret = Secret()
spec = AES.new(ecryptionKey, AES.MODE_CTR, iv=inputIV)
dataDec = cipher.decrypt(data)
第一个被注释掉的方法运行但给出了错误的结果。
第二个因错误而停止:
TypeError: Invalid parameters for CTR mode: {'iv': bytearray(b"-,R\x90\'X\x1b\x16(\x90\t9 \x00\x00\x00")}
在 C# 中,我们使用以下有效的方法:
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
ParametersWithIV ivAndKey = new ParametersWithIV(new KeyParameter(keyBytes), inputIVBytes);
cipher.Init(false, ivAndKey);
...
int length1 = cipher.ProcessBytes(data, 0, data.Length, outBuf, 0);
int length2 = cipher.DoFinal(outBuf, length1);
...
我很困惑,因为 C# 使用了我拥有的参数:key、data、IV
但是 Python 需要:键、数据、计数器
有没有人举例说明如何在 Python3 中解密它?或者解释一下我应该如何使用 IV 为 AES-CNT 设置计数器?
最后,https://www.programcreek.com/python/example/87998/Crypto.Cipher.AES.MODE_CTR 中的一个例子让我走上了正确的道路。在我的代码中修复了几个拼写错误后,它工作正常。
def kamstrupDecrypt(meterSerial, frame, payload):
inputIV = getInputIV(frame)
print("IV:", binascii.hexlify(inputIV))
ecryptionKey = getEncryptionKey(meterSerial)
print("Key:", binascii.hexlify(ecryptionKey))
counter = Counter.new(128, initial_value = bytes_to_long(inputIV))
cipher = AES.new(ecryptionKey, AES.MODE_CTR, counter=counter)
payloadDec = cipher.decrypt(payload)
print("Decrypted: ", binascii.hexlify(payloadDec))
return payloadDec
我有一个 Kamstrup WMbus 水表发送这样的帧:
21442D2C529027581B168D2814900939201F0775C6452FBBAC155B46A546035219D51AB8
我正在尝试在 Python 中解密。
打破框架我有以下值:
- 键
16ce383ebc0790e928215525cd4f7abf
- 输入 IV
2D2C529027581B162890093920000000
- 输入数据
1F0775C6452FBBAC155B46A546035219D5
将其粘贴到 http://www.cryptogrium.com/aes-ctr.html 中会生成有效的解密帧:
bbe57934ddc46a71004400000044000000
我已经尝试过 PyCrypto 和 PyCryptodome,但都给出了与 cryptogrium.com 相同的正确答案。
from Cryptodome.Cipher import AES
# ctr = Crypto.Util.Counter.new(128, initial_value=int("0000002039099028161B582790522C2D", 16))
# cipher = Crypto.Cipher.AES.new(ecryptionKey, Crypto.Cipher.AES.MODE_CTR, counter=ctr)
# secret = Secret()
spec = AES.new(ecryptionKey, AES.MODE_CTR, iv=inputIV)
dataDec = cipher.decrypt(data)
第一个被注释掉的方法运行但给出了错误的结果。 第二个因错误而停止:
TypeError: Invalid parameters for CTR mode: {'iv': bytearray(b"-,R\x90\'X\x1b\x16(\x90\t9 \x00\x00\x00")}
在 C# 中,我们使用以下有效的方法:
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
ParametersWithIV ivAndKey = new ParametersWithIV(new KeyParameter(keyBytes), inputIVBytes);
cipher.Init(false, ivAndKey);
...
int length1 = cipher.ProcessBytes(data, 0, data.Length, outBuf, 0);
int length2 = cipher.DoFinal(outBuf, length1);
...
我很困惑,因为 C# 使用了我拥有的参数:key、data、IV 但是 Python 需要:键、数据、计数器
有没有人举例说明如何在 Python3 中解密它?或者解释一下我应该如何使用 IV 为 AES-CNT 设置计数器?
最后,https://www.programcreek.com/python/example/87998/Crypto.Cipher.AES.MODE_CTR 中的一个例子让我走上了正确的道路。在我的代码中修复了几个拼写错误后,它工作正常。
def kamstrupDecrypt(meterSerial, frame, payload):
inputIV = getInputIV(frame)
print("IV:", binascii.hexlify(inputIV))
ecryptionKey = getEncryptionKey(meterSerial)
print("Key:", binascii.hexlify(ecryptionKey))
counter = Counter.new(128, initial_value = bytes_to_long(inputIV))
cipher = AES.new(ecryptionKey, AES.MODE_CTR, counter=counter)
payloadDec = cipher.decrypt(payload)
print("Decrypted: ", binascii.hexlify(payloadDec))
return payloadDec