So I have the below code in VueJS to encrypt certain credentials server-side before adding them as cookies for the user. The encrypted value is sent with requests to authenticate users between frontend and backend (python3.9 backend).
Now my question is how can I decrypt the encrypted value in python given that I have the salt, iv and SecretKey?
I tried playing around a bit with pyCryptoDome but couldn't figure out anything myself after about 1 hour of trying
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)
},
}
Python code: (not working)
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
))
it returns:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 4: invalid continuation byte
any help is appreciated