Skip to content

Commit

Permalink
Updated README.md and ran coverage/pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-rouleau committed Jan 13, 2024
1 parent 6f901e0 commit 03ea21f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -199,6 +235,7 @@ poetry run coverage run -m pytest -v
* <https://github.com/opendocument-app/OpenDocument.core/blob/233663b039/src/internal/ooxml/ooxml_crypto.h>
* <https://github.com/jaydadhania08/PHPDecryptXLSXWithPassword>
* <https://github.com/epicentre-msf/rpxl>
* <https://github.com/herumi/msoffice>

### In publications

Expand Down
6 changes: 2 additions & 4 deletions msoffcrypto/format/ooxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 03ea21f

Please sign in to comment.