From 03ea21f67ea7b771438f27de2798c645925e4c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Rouleau?= Date: Sat, 13 Jan 2024 13:53:45 -0500 Subject: [PATCH] Updated README.md and ran coverage/pytest --- README.md | 43 ++++++++++++++++++++++++++++++++++--- msoffcrypto/format/ooxml.py | 6 ++---- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4d6e3e6..239786f 100644 --- a/README.md +++ b/README.md @@ -47,11 +47,17 @@ Test if the file is encrypted or not (exit code 0 or 1 is returned): msoffcrypto-tool document.doc --test -v ``` +Encrypt an OOXML file: + +``` +msoffcrypto-tool -e -p Passw0rd plain.docx encrypted.docx +``` + ### As library Password and more key types are supported with library functions. -Basic usage: +Basic decryption usage: ```python import msoffcrypto @@ -67,7 +73,7 @@ with open("decrypted.docx", "wb") as f: encrypted.close() ``` -Basic usage (in-memory): +Basic decryption usage (in-memory): ```python import msoffcrypto @@ -85,6 +91,35 @@ df = pd.read_excel(decrypted) print(df) ``` +Basic encryption usage (only OOXML is supported): + +```python +from msoffcrypto.format.ooxml import OOXMLFile + +plain = open("plain.docx", "rb") +file = OOXMLFile(plain) + +with open("encrypted.docx", "wb") as f: + file.encrypt("Passw0rd", f) + +plain.close() +``` + +Basic encryption usage (in-memory, only OOXML is supported): + +```python +from msoffcrypto.format.ooxml import OOXMLFile +import io + +encrypted = io.BytesIO() + +with open("plain.xlsx", "rb") as f: + file = OOXMLFile(f) + file.encrypt("Passw0rd", encrypted) + +# Do stuff with encrypted buffer; it contains an OLE container with an encrypted stream +``` + Advanced usage: ```python @@ -155,7 +190,8 @@ poetry run coverage run -m pytest -v * [x] Improve error types (v4.12.0) * [ ] Redesign APIs (v6.0.0) * [ ] Introduce something like `ctypes.Structure` -* [ ] Support encryption +* [x] Support OOXML encryption +* [ ] Support other encryption * [ ] Isolate parser ## Resources @@ -199,6 +235,7 @@ poetry run coverage run -m pytest -v * * * +* ### In publications diff --git a/msoffcrypto/format/ooxml.py b/msoffcrypto/format/ooxml.py index 8556143..0441326 100644 --- a/msoffcrypto/format/ooxml.py +++ b/msoffcrypto/format/ooxml.py @@ -242,13 +242,11 @@ def decrypt(self, ofile, verify_integrity=False): def encrypt(self, password, ofile): """ - >>> from msoffcrypto import exceptions + >>> from msoffcrypto.format.ooxml import OOXMLFile >>> from io import BytesIO; ofile = BytesIO() - >>> with open("tests/outputs/ecma376standard_password_plain.docx", "rb") as f: + >>> with open("tests/outputs/example.docx", "rb") as f: ... officefile = OOXMLFile(f) ... officefile.encrypt("1234", ofile) - Traceback (most recent call last): - msoffcrypto.exceptions.EncryptionError: Unable to encrypt this file """ if self.is_encrypted(): raise exceptions.EncryptionError("File is already encrypted")