from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import binascii

def aes_decrypt(encrypted_hex, key):
    key_bytes = bytes.fromhex(key)
    encrypted_data = binascii.unhexlify(encrypted_hex)

    cipher = Cipher(algorithms.AES(key_bytes), modes.CBC(b'\x00' * 16), backend=default_backend())
    decryptor = cipher.decryptor()

    decrypted_data = decryptor.update(encrypted_data) + decryptor.finalize()

    return decrypted_data.decode('utf-8')

# Your encrypted hex string and key
#encrypted_hex = "07132d209c8b798a970ee5022d720090f3a7f1f538955d9936d6b42fe48a199c35f98ebcf781791d3ecaa1f6bc4434818af942ff610459fbd4bf698c2086d30c259f84cd3bed2cfcbebd375e4ef852b4"
encrypted_hex = "f8cac9f2d70056463ce528cf2ae836f4a411ae3453cf77ba6cea7060d59dc601f27ef9c59092a18257ccf3e6d1832c101b2999071ac183a1dd52ceff95f4e650"
key = "594162417237323352466D3964673233"

# Decrypt the data
decrypted_data = aes_decrypt(encrypted_hex, key)

print("Decrypted Data:", decrypted_data)



def aes_encrypt(data, key):
    key_bytes = bytes.fromhex(key)
    data_bytes = data.encode('ascii')

    # Use the same initialization vector (IV) as in your decryption function
    iv = b'\x00' * 16

    cipher = Cipher(algorithms.AES(key_bytes), modes.CBC(iv), backend=default_backend())
    encryptor = cipher.encryptor()

    encrypted_data = encryptor.update(data_bytes) + encryptor.finalize()

    return binascii.hexlify(encrypted_data).decode('ascii')

# Your original decrypted data and key
key = "535A6D6766584B4C75314238444E3237"
theData = "ABCDEFGHIJKL|9999|Nri1/BA01]_17:04:17,12-11-2024"

# Encrypt the data
encrypted_data = aes_encrypt(theData, key)

print("Encrypted Data:", encrypted_data)
