我正在尝试解密使用“ crypto-js”编码的字符串,并使用“ pyCrypto”在python中对其进行解码。我在各个博客上都遵循了确切的步骤,但是仍然存在相同的错误。
我关注的最后一个stackoverflow帖子是 “ CryptoJS and Pycrypto working together”是@Artjom B给出的答案。
还尝试了“ https://chase-seibert.github.io/blog/2016/01/29/cryptojs-pycrypto-ios-aes256.html”
我的js代码是
var pass = CryptoJS.AES.encrypt(text, password_encrypt_key,
{
iv: password_encrypt_iv,
})
return password_encrypt_iv.concat(pass.ciphertext).toString(CryptoJS.enc.Base64);
我的python代码是
BLOCK_SIZE = 16
KEY = constants.PASSWORD_ENCRYPT_KEY
# IV = constants.PASSWORD_ENCRYPT_IV
IV = enc_password[:BLOCK_SIZE]
MODE = AES.MODE_CBC
enc_password = base64.b64decode(enc_password)
aes = AES.new(KEY, MODE, IV)
password = unpad(aes.decrypt(enc_password[BLOCK_SIZE:]))
unpad功能
def unpad(s):
return s[:-ord(s[-1])]
答案 0 :(得分:0)
我找到了解决方案。不知道这是如何工作的,而不是解决方案的其余部分,但是无论如何都会发布它。解决方案也来自Artjom B的以下链接答案。他给出了更好的解释。我也在发布相同的答案。
链接-How to decrypt password from JavaScript CryptoJS.AES.encrypt(password, passphrase) in Python
Javascript-
var KEY = encrypt_key;
var encrypted_txt_obj = CryptoJS.AES.encrypt(text, KEY);
return encrypted_txt_obj.toString();
python-
from Crypto.Cipher import AES
import base64
BLOCK_SIZE = 16
def bytes_to_key(data, salt, output=48):
data += salt
key = md5(data).digest()
final_key = key
while len(final_key) < output:
key = md5(key + data).digest()
final_key += key
return final_key[:output]
def decrypt_text(enc):
try:
enc = base64.b64decode(enc)
assert enc[0:8] == b"Salted__"
salt = enc[8:16]
key_iv = bytes_to_key(encrypt_key, salt, 32 + 16)
key = key_iv[:32]
iv = key_iv[32:]
aes = AES.new(key, AES.MODE_CBC, iv)
text = unpad(aes.decrypt(enc[16:]))
return text
except Exception as e:
resp = jsonify({constants.ERR_SERVER: e.message})
resp.status_code = 403
logger.error("Exception %s", e.message)
return resp
def unpad(data):
return data[:-(data[-1] if type(data[-1]) == int else ord(data[-1]))]