added new python decryption script to encryption.md

This commit is contained in:
Guy B. deBros 2020-06-09 06:51:09 -04:00 committed by Jonathan Wren
parent ffe6e9070d
commit c0f7f22d09
No known key found for this signature in database
GPG key ID: 43D5FF8722E7F68A

View file

@ -1,7 +1,5 @@
# Encryption # Encryption
## `pycrypto`
Please note that _all_ of `jrnl`'s encryption functions require `pycrypto`, Please note that _all_ of `jrnl`'s encryption functions require `pycrypto`,
which can be installed using `pip`: which can be installed using `pip`:
@ -38,21 +36,21 @@ encryption password in your keychain. This saves you the trouble of having to
enter your password every time you want to write in or read your journal. enter your password every time you want to write in or read your journal.
If you don't initially store the password in the keychain but decide to do so at If you don't initially store the password in the keychain but decide to do so at
a later point---or if you want to store it in one computer's keychain but not a later point---or if you want to store it in one computer's keychain but not in
in another computer's---you can run `jrnl --encrypt` on an encrypted journal another computer's---you can run `jrnl --encrypt` on an encrypted journal and
and use the same password again. This will trigger the keychain storage prompt. use the same password again. This will trigger the keychain storage prompt.
## A Note on Security ## A Note on Security
While `jrnl` follows best practices, total security is never possible in the While `jrnl` follows best practices, total security is never possible in the
real world. There are a number of ways that people can at least partially real world. There are a number of ways that people can at least partially
compromise your `jrnl` data. See the [Privacy and Security](./security.md) compromise your `jrnl` data. See the [Privacy and Security](./security.md) page
page for more information. for more information.
## Password Recovery ## Password Recovery
There is no method to recover or reset your `jrnl` password. If you lose it, There is no method to recover or reset your `jrnl` password. If you lose it,
your data is inaccessible forever. your data will be inaccessible forever.
## Manual Decryption ## Manual Decryption
@ -67,6 +65,41 @@ Here is a Python script that you can use to decrypt your journal:
``` python ``` python
#!/usr/bin/env python3 #!/usr/bin/env python3
import base64
import getpass
from pathlib import Path
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
filepath = input("journal file path: ")
password = getpass.getpass("Password: ")
with open(Path(filepath),"rb") as f:
ciphertext = f.read()
password = password.encode("utf-8")
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=b"\xf2\xd5q\x0e\xc1\x8d.\xde\xdc\x8e6t\x89\x04\xce\xf8",
iterations=100_000,
backend=default_backend(),
)
key = base64.urlsafe_b64encode(kdf.derive(password))
print(Fernet(key).decrypt(ciphertext).decode('utf-8'))
```
If you're still using `jrnl` version 1.X, the following script serves the same
purpose:
``` python
#!/usr/bin/env python3
import argparse import argparse
from Crypto.Cipher import AES from Crypto.Cipher import AES
import getpass import getpass
@ -74,18 +107,18 @@ import hashlib
import sys import sys
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument(“filepath”, help=”journal file to decrypt”) parser.add_argument("filepath", help="journal file to decrypt")
args = parser.parse_args() args = parser.parse_args()
pwd = getpass.getpass() pwd = getpass.getpass()
key = hashlib.sha256(pwd.encode(utf-8)).digest() key = hashlib.sha256(pwd.encode('utf-8')).digest()
with open(args.filepath, rb) as f: with open(args.filepath, 'rb') as f:
ciphertext = f.read() ciphertext = f.read()
crypto = AES.new(key, AES.MODE_CBC, ciphertext[:16]) crypto = AES.new(key, AES.MODE_CBC, ciphertext[:16])
plain = crypto.decrypt(ciphertext[16:]) plain = crypto.decrypt(ciphertext[16:])
plain = plain.strip(plain[-1:]) plain = plain.strip(plain[-1:])
plain = plain.decode(“utf-8”) plain = plain.decode("utf-8")
print(plain) print(plain)
``` ```