from Crypto.Cipher import AES
import base64

def decrypt_aes_gcm(encrypted_base64, key_base64):
    # Decode the base64 encoded data and key
    encrypted_data = base64.b64decode(encrypted_base64)
    key = base64.b64decode(key_base64)

    # Extract the nonce, tag, and ciphertext from the combined encrypted data
    nonce = encrypted_data[:12]        # AES GCM nonce is 12 bytes
    tag = encrypted_data[-16:]         # AES GCM tag is 16 bytes
    ciphertext = encrypted_data[12:-16] # Ciphertext is everything in between

    # Create the cipher object and decrypt the data
    cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
    decrypted_data = cipher.decrypt_and_verify(ciphertext, tag)

    # Convert decrypted bytes to string (assuming UTF-8 encoding)
    decrypted_text = decrypted_data.decode('utf-8')
    return decrypted_text
