blob: cffd224dd7bbbe575389738364441ff456f7fca6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
=====================
Secret Key Encryption
=====================
Secret key encryption is the method of using a single key for both encryption
and decryption of messages. One of the classic examples from history of secret
key, or symmetric, encryption is the Enigma machine.
The SecretBoxEasy class in libnacl.secret_easy makes this type of encryption very easy
to execute:
.. code-block:: python
import libnacl.secret_easy
msg = b'But then of course African swallows are not migratory.'
# Create a SecretBox object, if not passed in the secret key is
# Generated purely from random data
box = libnacl.secret_easy.SecretBoxEasy()
# Messages can now be safely encrypted
ctxt = box.encrypt(msg)
# An additional box can be created from the original box secret key
box2 = libnacl.secret_easy.SecretBoxEasy(box.sk)
# Messages can now be easily encrypted and decrypted
clear1 = box.decrypt(ctxt)
clear2 = box2.decrypt(ctxt)
ctxt2 = box2.encrypt(msg)
clear3 = box.decrypt(ctxt2)
.. note::
Every encryption routine requires a nonce. The nonce is a 24 char string
that must never be used twice with the same keypair. If no nonce is passed
in then a nonce is generated based on random data.
If it is desired to generate a nonce manually this can be done by passing
it into the encrypt method.
|