Así que tengo el siguiente código en VueJS para cifrar ciertas credenciales del lado del servidor antes de agregarlas como cookies para el usuario. El valor cifrado se envía con solicitudes para autenticar a los usuarios entre el frontend y el backend (backend de python3.9).
Ahora mi pregunta es ¿cómo puedo descifrar el valor cifrado en python dado que tengo salt, iv y SecretKey?
Traté de jugar un poco con pyCryptoDome pero no pude resolver nada después de aproximadamente 1 hora de intentarlo.
const CryptoJS = require("crypto-js"); const salt = CryptoJS.enc.Utf8.parse('salt_here'); const iv = CryptoJS.enc.Utf8.parse('iv_here'); const key = CryptoJS.PBKDF2( 'SecretKey', CryptoJS.enc.Utf8.parse(salt), { keySize: 512 / 32, iterations: 1000 } ); const crypto = { encrypt: (data) => { return CryptoJS.AES.encrypt(data, key, { iv: iv }).toString() }, decrypt: (data) => { return CryptoJS.AES.decrypt(data, key, { iv: iv }).toString(CryptoJS.enc.Utf8) }, }Código Python: (no funciona)
from Crypto.Protocol.KDF import PBKDF2 def decrypt(_encrypted_text: str = "", _iv: str = "", _salt: str = "", _password: str = "") -> str: ''' IN: _encrypted_text <str, ""> - String of encrypted text to be decrypted OUT: <str, ""> - encrypted text now decrypted into plain text form. Decrypts ciphertext using AES256 algorithm ''' encrypted_text = base64.b64decode(_encrypted_text) iv = encrypted_text[:16] cipher = AES.new(_password, AES.MODE_CBC, iv) unpad = lambda s: s[:-ord(s[len(s) - 1:])] #plain_text decrypted = unpad(cipher.decrypt(encrypted_text[16:])).decode("utf-8") #ensure it is converted to float or int and doesn't stay as a string return fast_real(decrypted) salt = b"salt_here" iv = b"iv_here" key = PBKDF2(b'SecretKey', salt, int(512/32), 1000) print(decrypt( _encrypted_text = enc, _iv = iv, _salt = salt, _password = key ))vuelve:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 4: invalid continuation byte
cualquier ayuda es apreciada