blob: f79bcacb08650b5e8a23267e02bf5b2e76da44e6 [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 Gaynorc651f762013-08-08 11:17:56 -070054 in a transmitted message). Should be
Alex Gaynora1b98f92013-08-08 11:37:25 -070055 the same number of bytes as the
56 ``block_size`` of the cipher.