AES-将Javascript解密算法转换为Python



我试图将Javascript解密算法转换为Python,但得到了ValueError: Incorrect AES key length (64 bytes)

Javascript版本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
<script>
//------------------------------------------------------------------------------------------------------------
//JS compatible decryption algo
//------------------------------------------------------------------------------------------------------------
function decrypt(cypher_text, keyIn, IVIn){
var iv = CryptoJS.enc.Hex.parse(IVIn);
var key = CryptoJS.enc.Hex.parse(keyIn);
var ct = CryptoJS.enc.Base64.parse(cypher_text);
var decrypted = CryptoJS.AES.decrypt({ciphertext: ct, salt: ""}, key, {iv: iv}).toString(CryptoJS.enc.Utf8);
return decrypted;
}
var key = "3166626465356135633961303634353662393166363430306633656365366162";
var ciphertext = "WCJcIFWu+3gC3nesXiiVcvXsVB104J10ZBVky2KIg1pXO9AWoI45ClkWg5EkW/3aBHGPcE5F7bXf8W0dtn8UcrJv7reR1dETQX9cgO7qYPonmNNrbdJprxR/2Z/e3AJGUGrhsjZC8IaiL3mM7+sHSAIm1lsusk9ZjJchpxm00CHQh698ILdG9+8/FCSNTJ3QTm9vy8BDmt8XmGl95ORcryUleBLfI2VpY9cNKzXLIYBBBD8OoDH8ASqh5LSb/PQTr96cYJJQG+3P+xvg623pJxuzMXgoqZuDXaoY082fzWGuaU6Pr8zKI6DxMjBX4j6sCo2Csilma6MG6zJWMQ6QWdG+ulilvLluolwiCECLqd4qbEeCBBJ7oagjr5sdCz2lNX/E";
var IV = "111d8a663094d022a548d5e79670be37";
var decrypted = decrypt(ciphertext, key, IV);
</script>
<!DOCTYPE html>
<html>
<body>
<script>
document.write(decrypted);
</script>
</body>
</html>

输出(Javascript版本(:

"[{"record_index":15145877,"unit_id":"AWTIRDMVAPU397","tag_id":3461

Python版本:

# decrypting the data
# decryption
key = "3166626465356135633961303634353662393166363430306633656365366162"
iv = "111d8a663094d022a548d5e79670be37"
ciphertext = "WCJcIFWu+3gC3nesXiiVcvXsVB104J10ZBVky2KIg1pXO9AWoI45ClkWg5EkW/3aBHGPcE5F7bXf8W0dtn8UcrJv7reR1dETQX9cgO7qYPonmNNrbdJprxR/2Z/e3AJGUGrhsjZC8IaiL3mM7+sHSAIm1lsusk9ZjJchpxm00CHQh698ILdG9+8/FCSNTJ3QTm9vy8BDmt8XmGl95ORcryUleBLfI2VpY9cNKzXLIYBBBD8OoDH8ASqh5LSb/PQTr96cYJJQG+3P+xvg623pJxuzMXgoqZuDXaoY082fzWGuaU6Pr8zKI6DxMjBX4j6sCo2Csilma6MG6zJWMQ6QWdG+ulilvLluolwiCECLqd4qbEeCBBJ7oagjr5sdCz2lNX/E"
#print(ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(ciphertext)
decryptedUnpad = unpad(decrypted, AES.block_size)
# print(decryptedUnpad)
content = decryptedUnpad
print(str(content))

输出(Python-ValueError: Incorrect AES key length (64 bytes)(:

Traceback (most recent call last):
File "C:/Users/Admin/Desktop/TestProp/main.py", line 192, in <module>
cipher = AES.new(key, AES.MODE_CBC, iv)
File "C:UsersAdminDesktopTestPropvenvlibsite-packagesCryptoCipherAES.py", line 232, in new
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
File "C:UsersAdminDesktopTestPropvenvlibsite-packagesCryptoCipher__init__.py", line 79, in _create_cipher
return modes[mode](factory, **kwargs)
File "C:UsersAdminDesktopTestPropvenvlibsite-packagesCryptoCipher_mode_cbc.py", line 274, in _create_cbc_cipher
cipher_state = factory._create_base_cipher(kwargs)
File "C:UsersAdminDesktopTestPropvenvlibsite-packagesCryptoCipherAES.py", line 93, in _create_base_cipher
raise ValueError("Incorrect AES key length (%d bytes)" % len(key))
ValueError: Incorrect AES key length (64 bytes)

我在这里错过了什么?

您缺少相应的python步骤来准备来自hexb64的数据

var iv = CryptoJS.enc.Hex.parse(IVIn);    
var key = CryptoJS.enc.Hex.parse(keyIn);    
var ct = CryptoJS.enc.Base64.parse(cypher_text);
key = bytearray.fromhex(key)
iv = bytearray.fromhex(iv)
ciphertext = b64decode(ciphertext)

你的数据似乎被截断了,所以我删除了最后4个b64字符,以便能够正确解密

from base64 import b64decode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
key = "3166626465356135633961303634353662393166363430306633656365366162"
iv = "111d8a663094d022a548d5e79670be37"
ciphertext = "WCJcIFWu+3gC3nesXiiVcvXsVB104J10ZBVky2KIg1pXO9AWoI45ClkWg5EkW/3aBHGPcE5F7bXf8W0dtn8UcrJv7reR1dETQX9cgO7qYPonmNNrbdJprxR/2Z/e3AJGUGrhsjZC8IaiL3mM7+sHSAIm1lsusk9ZjJchpxm00CHQh698ILdG9+8/FCSNTJ3QTm9vy8BDmt8XmGl95ORcryUleBLfI2VpY9cNKzXLIYBBBD8OoDH8ASqh5LSb/PQTr96cYJJQG+3P+xvg623pJxuzMXgoqZuDXaoY082fzWGuaU6Pr8zKI6DxMjBX4j6sCo2Csilma6MG6zJWMQ6QWdG+ulilvLluolwiCECLqd4qbEeCBBJ7oagjr5sdCz2l"
key = bytearray.fromhex(key)
iv = bytearray.fromhex(iv)
ciphertext = b64decode(ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(ciphertext)
print(decrypted)
# b'"[{\"record_index\":15145877,\"unit_id\":\"AWTIRDMVAPU397\",\"tag_id\":3461,\"alarms\":{\"Track Mode\":false,\"Battery\":false,\"Geofence\":false,\"Coverage\":true,\"Memory\":false,\"CBit\":false,\"Moveme
# nt\":\"Unknown\",\"Tamperfoil\":false},\"batt\":3.6800000000000002,\"temperature\":2'

只有当数据在加密之前被填充时,该代码才会有用

decryptedUnpad = unpad(decrypted, AES.block_size) 
print(decryptedUnpad)

相关内容

  • 没有找到相关文章

最新更新