To decrypt data encrypted with CryptoJS.AES in Python, you need to understand how CryptoJS formats its encrypted output.
CryptoJS uses a format like:
code
Salted__<8-byte salt><ciphertext>
So the encrypted string (usually Base64-encoded) starts with the literal "Salted__" followed by 8 bytes of salt, and then the actual ciphertext.
What You Need:
- The Base64-encoded encrypted string from CryptoJS
- The passphrase (not a raw key — CryptoJS derives the key and IV from the passphrase and salt)
✅ Python Decryption Steps
We'll use pycryptodome to replicate CryptoJS's key/IV derivation.
1. Install dependencies
bash
pip install pycryptodome
2. Python Code
python
import base64
import hashlib
from Crypto.Cipher import AES
def decrypt_cryptojs_aes(base64_encrypted: str, passphrase: str) -> str:
# Decode base64
data = base64.b64decode(base64_encrypted)
# Check for "Salted__" prefix
if data[:8] != b'Salted__':
raise ValueError("Invalid CryptoJS format")
salt = data[8:16]
ciphertext = data[16:]
# Derive key and IV (same as CryptoJS)
passphrase_bytes = passphrase.encode('utf-8')
key_iv = hashlib.pbkdf2_hmac('sha1', passphrase_bytes, salt, 1000, 48)
key = key_iv[:32]
iv = key_iv[32:48]
# Decrypt
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(ciphertext)
# Remove PKCS7 padding
padding_length = decrypted[-1]
decrypted = decrypted[:-padding_length]
return decrypted.decode('utf-8')
# Example usage
base64_encrypted = "U2FsdGVkX1+..." # your encrypted string here
passphrase = "your-secret-key"
try:
result = decrypt_cryptojs_aes(base64_encrypted, passphrase)
print("Decrypted:", result)
except Exception as e:
print("Decryption failed:", e)
🔍 Notes
- This assumes AES-256-CBC (CryptoJS default).
- If your CryptoJS used a different mode (like CTR), this won't work.
- If you used a raw key and IV instead of a passphrase, let me know — the process is different.
If you share your encrypted string and key (or confirm it's a passphrase), I can help verify the format.