Last active
October 26, 2018 15:43
-
-
Save authmane512/2def44e2cab214815d002d4b0d1fbb1e to your computer and use it in GitHub Desktop.
How to securely decrypt data in Python with NaCl library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nacl.secret | |
import nacl.utils | |
import nacl.pwhash | |
import base64 | |
password = b"I like Python" | |
salt = b'3\xba\x8f\r]\x1c\xcbOsU\x12\xb6\x9c(\xcb\x94' # our previous salt from encryption.py: | |
# https://gist.github.com/authmane512/7f0c1d6797ea9ff83015c3ddce704b3a | |
# Generate the key: | |
kdf = nacl.pwhash.argon2i.kdf # our key derivation function | |
key = kdf(nacl.secret.SecretBox.KEY_SIZE, password, salt) | |
# Read the data with binary mode: | |
with open('file.bin', 'rb') as f: | |
encrypted = f.read() | |
# Read the data with text mode: | |
with open('file.txt', 'r') as f: | |
content = f.read() | |
encrypted = base64.b64decode(content) | |
# Decrypt the data: | |
box = nacl.secret.SecretBox(key) | |
secret_msg = box.decrypt(encrypted) | |
print(secret_msg.decode("utf-8")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the code for encryption:
https://gist.github.com/authmane512/7f0c1d6797ea9ff83015c3ddce704b3a