Skip to content

Instantly share code, notes, and snippets.

@authmane512 authmane512/

Last active Oct 26, 2018
What would you like to do?
How to securely decrypt data in Python with NaCl library
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
# 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 =
# Read the data with text mode:
with open('file.txt', 'r') as f:
content =
encrypted = base64.b64decode(content)
# Decrypt the data:
box = nacl.secret.SecretBox(key)
secret_msg = box.decrypt(encrypted)

This comment has been minimized.

Copy link
Owner Author

authmane512 commented Oct 26, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.