Secret Key Encryption¶
Secret key encryption (also called symmetric key encryption) is analogous to a
safe. You can store something secret through it and anyone who has the key can
open it and view the contents.
SecretBox functions as
just such a safe, and like any good safe any attempts to tamper with the
contents is easily detected.
Secret key encryption allows you to store or transmit data over insecure channels without leaking the contents of that message, nor anything about it other than the length.
import nacl.secret import nacl.utils # This must be kept secret, this is the combination to your safe key = nacl.utils.random(nacl.secret.SecretBox.KEY_SIZE) # This is your safe, you can use it to encrypt or decrypt messages box = nacl.secret.SecretBox(key) # This is our message to send, it must be a bytestring as SecretBox will # treat it as just a binary blob of data. message = b"The president will be exiting through the lower levels"
PyNaCl can automatically generate a random nonce for us, making the encryption very simple:
# Encrypt our message, it will be exactly 40 bytes longer than the # original message as it stores authentication information and the # nonce alongside it. encrypted = box.encrypt(message)
However, if we need to use an explicit nonce, it can be passed along with the message:
# This is a nonce, it *MUST* only be used once, but it is not considered # secret and can be transmitted or stored alongside the ciphertext. A # good source of nonces are just sequences of 24 random bytes. nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE) encrypted = box.encrypt(message, nonce)
Finally, the message is decrypted (regardless of how the nonce was generated):
# Decrypt our message, an exception will be raised if the encryption was # tampered with or there was otherwise an error. plaintext = box.decrypt(encrypted)
The 32 bytes key given to
SecretBox must be kept secret.
It is the combination to your “safe” and anyone with this key will be able to
decrypt the data, or encrypt new data.
The 24-byte nonce (Number used once)
decrypt() must NEVER be reused for a
particular key. Reusing a nonce may give an attacker enough information to
decrypt or forge other messages. A nonce is not considered secret and may be
freely transmitted or stored in plaintext alongside the ciphertext.
A nonce does not need to be random or unpredictable, nor does the method of generating them need to be secret. A nonce could simply be a counter incremented with each message encrypted, which can be useful in connection-oriented protocols to reject duplicate messages (“replay attacks”). A bidirectional connection could use the same key for both directions, as long as their nonces never overlap (e.g. one direction always sets the high bit to “1”, the other always sets it to “0”).
If you use a counter-based nonce along with a key that is persisted from one session to another (e.g. saved to disk), you must store the counter along with the key, to avoid accidental nonce reuse on the next session. For this reason, many protocols derive a new key for each session, reset the counter to zero with each new key, and never store the derived key or the counter.
You can safely generate random nonces by calling
The SecretBox class encrypts and decrypts messages using the given secret key.
The ciphertexts generated by
Secretboxinclude a 16 byte authenticator which is checked as part of the decryption. An invalid authenticator will cause the decrypt function to raise an exception. The authenticator is not a signature. Once you’ve decrypted the message you’ve demonstrated the ability to create arbitrary valid message, so messages you send are repudiable. For non-repudiable messages, sign them after encryption.
- key (bytes) – The secret key used to encrypt and decrypt messages.
- encoder – A class that is able to decode the
encrypt(plaintext, nonce, encoder)¶
Encrypts the plaintext message using the given nonce (or generates one randomly if omitted) and returns the ciphertext encoded with the encoder.
It is VITALLY important that the nonce is a nonce, i.e. it is a number used only once for any given key. If you fail to do this, you compromise the privacy of the messages encrypted. Give your nonces a different prefix, or have one side use an odd counter and one an even counter. Just make sure they are different.
- plaintext (bytes) – The plaintext message to encrypt.
- nonce (bytes) – The nonce to use in the encryption.
- encoder – A class that is able to decode the ciphertext.
An instance of
decrypt(ciphertext, nonce, encoder)¶
Decrypts the ciphertext using the nonce (explicitly, when passed as a parameter or implicitly, when omitted, as part of the ciphertext) and returns the plaintext message.
- ciphertext (bytes) – The encrypted message to decrypt.
- nonce (bytes) – The nonce to use in the decryption.
- encoder – A class that is able to decode the plaintext.
The decrypted plaintext.
|Encryption:||Salsa20 stream cipher|