msoffcrypto-tool
msoffcrypto-tool copied to clipboard
The xls file generates a different error message when using decrypt compared to other files.
problem description
Recently, I was writing a script to decrypt my files in batches. Since some of the files were not encrypted, I needed to catch the DecryptionError ("Unencrypted document") error message. This error message was expected and occurred consistently in xlsx, docx, and doc formats. However, in xls format, a different type of error message, ParseError("Record not found"), appeared. To handle all situations, I need to write more code.
example
for aim_file_path in self.file_path_list:
for key in self.key_list:
output_load = output + str(datetime.datetime.now().timestamp()) + "_" + os.path.basename(aim_file_path)
try:
encrypted = open(aim_file_path, "rb")
file = msoffcrypto.OfficeFile(encrypted)
file.load_key(key)
with open(output_load, "wb") as f:
file.decrypt(f)
encrypted.close()
self.buf_list.append(os.path.basename(output_load))
except DecryptionError as e:
print(e)
if str(e.args[0]) == "Unencrypted document":
self.buf_list.append(os.path.basename(os.path.basename(output_load)))
shutil.copyfile(aim_file_path, output_load)
break
except ParseError as e:
print(e)
if str(e.args[0]) == "Record not found":
self.buf_list.append(os.path.basename(os.path.basename(output_load)))
shutil.copyfile(aim_file_path, output_load)
break
I have only used this library for a short time and may not have much experience yet. Why not design the error messages to be the same? It is hoped that these problems can be solved in future versions.