所以我試圖解密我在 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)
...
通過這兩個更改之一,解密就可以進行。
添加回答
舉報
0/150
提交
取消