blob: 39a5a630d9a322f7f375133482dfcac28c118bc0 [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 Gaynor4dd1c272013-08-08 11:39:21 -070016 >>> from cryptography.primitives.block import BlockCipher, cipher, mode
17 >>> cipher = BlockCipher(cipher.AES(key), mode.CBC(iv))
Alex Gaynorf6c47e92013-08-08 07:16:01 -070018 >>> cipher.encrypt("my secret message") + cipher.finalize()
19 # The ciphertext
20 [...]
21
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070022Here ``key`` is the encryption key (which must be kept secret), and ``iv`` is
Alex Gaynore7869432013-08-08 07:39:26 -070023the initialization vector (which must be random). Exactly what form these
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070024values should take is described for each of the ciphers and modes.
25
26``encrypt()`` should be called repeatedly with additional plaintext, and it
27will return the encrypted bytes, if there isn't enough data, it will buffer it
28internally. ``finalize()`` should be called at the end, and will return
29whatever data is left.
Alex Gaynord96d1002013-08-08 07:37:26 -070030
31Ciphers
32~~~~~~~
33
Alex Gaynor4dd1c272013-08-08 11:39:21 -070034.. class:: cryptography.primitives.block.cipher.AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070035
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070036 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070037 AES is both fast, and cryptographically strong. It is a good default
38 choice for encryption.
39
40 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070041 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070042
Alex Gaynord96d1002013-08-08 07:37:26 -070043
44Modes
45~~~~~
46
Alex Gaynor4dd1c272013-08-08 11:39:21 -070047.. class:: cryptography.primitives.block.mode.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070048
49 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
50 considered cryptographically strong.
51
52 :param bytes initialization_vector: Must be random bytes. They do not need
53 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -070054 in a transmitted message). Must be the
55 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -070056 ``block_size`` of the cipher. Do not
57 reuse an ``initialization_vector`` with
58 a given ``key``.