To decrypt CryptoJS AES encrypted data in Python, you'll need to understand how CryptoJS handles encryption. CryptoJS uses specific defaults that you'll need to replicate in Python.
Here's how to decrypt CryptoJS AES encrypted data in Python:
import base64
from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Util.Padding import unpad
import hashlib
def decrypt_cryptojs_aes(encrypted_data, passphrase):
"""
Decrypt data that was encrypted using CryptoJS.AES.encrypt()
Args:
encrypted_data: Base64 encoded string from CryptoJS
passphrase: The password/key used for encryption
Returns:
Decrypted string
"""
# Decode the base64 encrypted data
encrypted_bytes = base64.b64decode(encrypted_data)
# CryptoJS format: "Salted__" + 8 bytes salt + encrypted data
assert encrypted_bytes[:8] == b'Salted__', "Invalid CryptoJS format"
# Extract salt and ciphertext
salt = encrypted_bytes[8:16]
ciphertext = encrypted_bytes[16:]
# CryptoJS uses PBKDF2 to derive key and IV from passphrase
# Default: 256-bit key, 128-bit IV, 1 iteration
key_iv = PBKDF2(passphrase, salt, dkLen=48, count=1, hmac_hash_module=hashlib.md5)
key = key_iv[:32] # First 32 bytes for key
iv = key_iv[32:48] # Next 16 bytes for IV
# Create cipher and decrypt
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(ciphertext)
# Remove PKCS7 padding
decrypted = unpad(decrypted, AES.block_size)
return decrypted.decode('utf-8')
# Alternative: If you have a direct key (not a passphrase)
def decrypt_cryptojs_aes_with_key(encrypted_data, key, iv):
"""
Decrypt when you have the actual key and IV (not a passphrase)
Args:
encrypted_data: Base64 encoded ciphertext
key: 32-byte key (for AES-256)
iv: 16-byte initialization vector
Returns:
Decrypted string
"""
# Decode base64
ciphertext = base64.b64decode(encrypted_data)
# Create cipher and decrypt
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(ciphertext)
# Remove padding
decrypted = unpad(decrypted, AES.block_size)
return decrypted.decode('utf-8')
# Example usage:
if __name__ == "__main__":
# Example 1: With passphrase (most common CryptoJS usage)
encrypted = "U2FsdGVkX1+ZLz3qQ6bXwjKniAiTO3J7HdZr3XwVnXk=" # Example
passphrase = "my-secret-password"
try:
decrypted = decrypt_cryptojs_aes(encrypted, passphrase)
print(f"Decrypted: {decrypted}")
except Exception as e:
print(f"Decryption failed: {e}")
# Example 2: With direct key and IV
# If you have hex strings for key and IV:
key_hex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
iv_hex = "0123456789abcdef0123456789abcdef"
key = bytes.fromhex(key_hex)
iv = bytes.fromhex(iv_hex)
encrypted_data = "your_base64_encrypted_data_here"
# decrypted = decrypt_cryptojs_aes_with_key(encrypted_data, key, iv)
Key points to remember:
-
CryptoJS Default Format: When using CryptoJS.AES.encrypt(text, passphrase), it creates a specific format: "Salted__" + 8-byte salt + encrypted data, all base64 encoded.
-
Key Derivation: CryptoJS uses PBKDF2 with MD5 as the hash function, 1 iteration, to derive both the key and IV from the passphrase and salt.
-
Padding: CryptoJS uses PKCS7 padding by default.
-
Mode: The default mode is CBC (Cipher Block Chaining).
If your CryptoJS code uses different settings (like a different mode, key size, or iteration count), you'll need to adjust the Python code accordingly. Let me know if you need help with a specific CryptoJS configuration!