Skip to content

Commit

Permalink
Fix errors for unencrypted documents
Browse files Browse the repository at this point in the history
  • Loading branch information
nolze committed Jul 4, 2021
1 parent 7737e03 commit 8b78a98
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions msoffcrypto/format/ooxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ def __init__(self, file):
elif self.type == "extensible":
pass
elif zipfile.is_zipfile(file):
self.file = file
self.type, self.info = None, None
self.secret_key = None
raise exceptions.FileFormatError("Unencrypted document or unsupported file format")
else:
raise exceptions.FileFormatError("Unsupported file format")

Expand Down Expand Up @@ -170,6 +168,8 @@ def load_key(self, password=None, private_key=None, secret_key=None, verify_pass
raise exceptions.DecryptionError("Unsupported key type for the encryption method")
elif secret_key:
self.secret_key = secret_key
else:
raise exceptions.DecryptionError("No key specified")

def decrypt(self, ofile, verify_integrity=False):
if self.type == "agile":
Expand All @@ -193,6 +193,8 @@ def decrypt(self, ofile, verify_integrity=False):
with self.file.openstream("EncryptedPackage") as stream:
obuf = ECMA376Standard.decrypt(self.secret_key, stream)
ofile.write(obuf)
else:
raise exceptions.DecryptionError("Unsupported encryption method")

# If the file is successfully decrypted, there must be a valid OOXML file, i.e. a valid zip file
if not zipfile.is_zipfile(io.BytesIO(obuf)):
Expand Down

0 comments on commit 8b78a98

Please sign in to comment.