Refactoring

This commit is contained in:
KnugiHK
2022-01-17 12:06:15 +08:00
parent c73eabe2a4
commit 9fe6a0d2a8
2 changed files with 16 additions and 7 deletions

View File

@@ -104,10 +104,19 @@ def main():
key = open(options.key, "rb").read() key = open(options.key, "rb").read()
db = open(options.backup, "rb").read() db = open(options.backup, "rb").read()
is_crypt14 = False if "crypt12" in options.backup else True is_crypt14 = False if "crypt12" in options.backup else True
if not extract.decrypt_backup(db, key, msg_db, is_crypt14): error = extract.decrypt_backup(db, key, msg_db, is_crypt14)
print("Dependencies of decrypt_backup are not " if error != 0:
"present. For details, see README.md") if error == 1:
exit(3) print("Dependencies of decrypt_backup are not "
"present. For details, see README.md.")
exit(3)
elif error == 2:
print("Failed when decompressing the decrypted backup."
"Possibly incorrect offsets used in decryption.")
exit(4)
else:
print("Unknown error occurred.")
exit(5)
if options.wa is None: if options.wa is None:
contact_db = "wa.db" contact_db = "wa.db"
else: else:

View File

@@ -37,7 +37,7 @@ def determine_day(last, current):
def decrypt_backup(database, key, output, crypt14=True): def decrypt_backup(database, key, output, crypt14=True):
if not support_backup: if not support_backup:
return False return 1
if len(key) != 158: if len(key) != 158:
raise ValueError("The key file must be 158 bytes") raise ValueError("The key file must be 158 bytes")
t1 = key[30:62] t1 = key[30:62]
@@ -62,11 +62,11 @@ def decrypt_backup(database, key, output, crypt14=True):
try: try:
db = zlib.decompress(db_compressed) db = zlib.decompress(db_compressed)
except zlib.error: except zlib.error:
print("Decompressing failed. Possibly incorrect offsets used in decryption.") return 2
if db[0:6].upper() == b"SQLITE": if db[0:6].upper() == b"SQLITE":
with open(output, "wb") as f: with open(output, "wb") as f:
f.write(db) f.write(db)
return True return 0
else: else:
raise ValueError("The plaintext is not a SQLite database. Did you use the key to encrypt something...") raise ValueError("The plaintext is not a SQLite database. Did you use the key to encrypt something...")