Python-Kamstrup总线解密

2024-10-03 19:25:41 发布

您现在位置: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)

第一个注释掉的方法运行,但给出了错误的结果。 第二个错误停止:

^{pr2}$

我们正在使用以下作品:

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期望:key、data、counter

有没有人举个例子,我可以在Python3中解密这个?或者解释一下我应该如何使用静脉注射来设置AES-CNT的计数器?在


Tags: comnewdatamodecountercryptointaes
1条回答
网友
1楼 · 发布于 2024-10-03 19:25:41

最后,来自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

相关问题 更多 >