From 647e406ac0591b86ca484bd7ad0f79cc97cd3875 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Thu, 8 Jan 2026 23:57:02 +0800 Subject: [PATCH] Implement early key validation via authenticated decryption (#190) Utilize `decrypt_and_verify` to immediately identify incorrect user-provided keys via GCM tag validation. --- Whatsapp_Chat_Exporter/android_crypt.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Whatsapp_Chat_Exporter/android_crypt.py b/Whatsapp_Chat_Exporter/android_crypt.py index 3e921d1..ed84041 100644 --- a/Whatsapp_Chat_Exporter/android_crypt.py +++ b/Whatsapp_Chat_Exporter/android_crypt.py @@ -112,8 +112,20 @@ def _decrypt_database(db_ciphertext: bytes, main_key: bytes, iv: bytes) -> bytes zlib.error: If decompression fails. ValueError: if the plaintext is not a SQLite database. """ + FOOTER_SIZE = 32 + if len(db_ciphertext) <= FOOTER_SIZE: + raise ValueError("Input data too short to contain a valid GCM tag.") + + actual_ciphertext = db_ciphertext[:-FOOTER_SIZE] + tag = db_ciphertext[-FOOTER_SIZE: -FOOTER_SIZE + 16] + cipher = AES.new(main_key, AES.MODE_GCM, iv) - db_compressed = cipher.decrypt(db_ciphertext) + try: + db_compressed = cipher.decrypt_and_verify(actual_ciphertext, tag) + except ValueError: + # This could be key, IV, or tag is wrong, but likely the key is wrong. + raise ValueError("Decryption/Authentication failed. Ensure you are using the correct key.") + db = zlib.decompress(db_compressed) if db[0:6].upper() != b"SQLITE": raise ValueError(