blob: 182c9e4690414db96c3186cbd4867e26634ea51f [file] [log] [blame]
Alex Gaynorf6c47e92013-08-08 07:16:01 -07001Symmetric Encryption
2====================
3
4Symmetric encryption is a way to encrypt (hide the plaintext value) material
5where the encrypter and decrypter both use the same key.
6
7Block ciphers
8-------------
9
10Block ciphers work by encrypting content in chunks, often 64- or 128-bits. They
11combine an underlying algorithm (such as AES), with a mode (such as CBC, CTR,
12or GCM). A simple example of encrypting content with AES is:
13
14.. code-block:: pycon
15
Alex Gaynorf6c47e92013-08-08 07:16:01 -070016 >>> from cryptography.primitives.aes import AES
Alex Gaynora4f529e2013-08-08 11:29:06 -070017 >>> from cryptography.primitives.block import BlockCipher, CBC
Alex Gaynorf6c47e92013-08-08 07:16:01 -070018 >>> cipher = BlockCipher(AES(key), CBC(iv))
19 >>> cipher.encrypt("my secret message") + cipher.finalize()
20 # The ciphertext
21 [...]
22
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070023Here ``key`` is the encryption key (which must be kept secret), and ``iv`` is
Alex Gaynore7869432013-08-08 07:39:26 -070024the initialization vector (which must be random). Exactly what form these
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070025values should take is described for each of the ciphers and modes.
26
27``encrypt()`` should be called repeatedly with additional plaintext, and it
28will return the encrypted bytes, if there isn't enough data, it will buffer it
29internally. ``finalize()`` should be called at the end, and will return
30whatever data is left.
Alex Gaynord96d1002013-08-08 07:37:26 -070031
32Ciphers
33~~~~~~~
34
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070035.. class:: cryptography.primitives.aes.AES(key)
36
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070037 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070038 AES is both fast, and cryptographically strong. It is a good default
39 choice for encryption.
40
41 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070042 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070043
Alex Gaynord96d1002013-08-08 07:37:26 -070044
45Modes
46~~~~~
47
Alex Gaynora4f529e2013-08-08 11:29:06 -070048.. class:: cryptography.primitives.block.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070049
50 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
51 considered cryptographically strong.
52
53 :param bytes initialization_vector: Must be random bytes. They do not need
54 to be kept secret (they can be included
Alex Gaynorc651f762013-08-08 11:17:56 -070055 in a transmitted message). Should be
56 the same number of bytes as the ``key``
57 for the cipher.
Alex Gaynor1c1bad62013-08-08 11:27:47 -070058 ``initialization_vectors`` should never
59 be reused across different messages.