为了账号安全,请及时绑定邮箱和手机立即绑定

将 AES 生成的十六进制从 aes-js(Javascript) 解密到 pycryptodome

将 AES 生成的十六进制从 aes-js(Javascript) 解密到 pycryptodome

侃侃尔雅 2023-07-18 15:44:41
所以我试图解密我在 Python 中用 JS 加密的字符串。我用过aes-js图书馆。我明白了:caba6777379a00d12dcd0447015cd4dbcba649857866072d。这是我的JS代码:var key = aesjs.utils.utf8.toBytes("ThisKeyIs16Bytes");console.log(`Key (bytes): ${key}`);var text = 'psst... this is a secret';var textBytes = aesjs.utils.utf8.toBytes(text);var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));var encryptedBytes = aesCtr.encrypt(textBytes);var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);console.log(`Hex: ${key}`);我已经在 python 中尝试了一些东西,但这就是我目前所拥有的:from Crypto.Cipher import AESciphered_data = bytearray.fromhex('caba6777379a00d12dcd0447015cd4dbcba649857866072d')key = b'ThisKeyIs16Bytes'cipher = AES.new(key, AES.MODE_CTR)original_data = cipher.decrypt(ciphered_data)print(original_data.decode("utf-8", errors="ignore"))但我收到的只是一团糟。=*լ☻ve↕-:tQɊ#¶。
查看完整描述

1 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

使用CTR模式。在 Pyton 代码中,缺少计数器的初始化,即正确起始值的定义,例如

...
cipher = AES.new(key, AES.MODE_CTR, nonce = b'', initial_value = 5)
...

或者使用一个Counter对象:

from Crypto.Util import Counter

...

counter = Counter.new(128, initial_value = 5)

cipher = AES.new(key, AES.MODE_CTR, counter = counter)

...

通过这两个更改之一,解密就可以进行。


查看完整回答
反对 回复 2023-07-18
  • 1 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信