blob: fb1a7cfc70e0142bdd15a75ef80ccfa8c12b47e7 [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
Alex Gaynor65678d02013-08-08 15:19:19 -07007.. class:: cryptography.primitives.block.BlockCipher(cipher, mode)
Alex Gaynorf6c47e92013-08-08 07:16:01 -07008
Alex Gaynor65678d02013-08-08 15:19:19 -07009 Block ciphers work by encrypting content in chunks, often 64- or 128-bits.
10 Theycombine an underlying algorithm (such as AES), with a mode (such as CBC,
11 CTR, or GCM). A simple example of encrypting content with AES is:
Alex Gaynorf6c47e92013-08-08 07:16:01 -070012
Alex Gaynor65678d02013-08-08 15:19:19 -070013 .. code-block:: pycon
Alex Gaynorf6c47e92013-08-08 07:16:01 -070014
Alex Gaynor65678d02013-08-08 15:19:19 -070015 >>> from cryptography.primitives.block import BlockCipher, cipher, mode
16 >>> cipher = BlockCipher(cipher.AES(key), mode.CBC(iv))
17 >>> cipher.encrypt("my secret message") + cipher.finalize()
18 # The ciphertext
19 [...]
Alex Gaynorf6c47e92013-08-08 07:16:01 -070020
Alex Gaynore62aa402013-08-08 15:23:11 -070021 :param cipher: One of the ciphers described below.
22 :param mode: One of the modes described below.
Alex Gaynor0ca7fdb2013-08-08 07:35:26 -070023
Alex Gaynor09515f02013-08-08 15:26:55 -070024 ``encrypt()`` should be called repeatedly with new plaintext, and once the
25 full plaintext is fed in, ``finalize()`` should be called.
26
Alex Gaynore62aa402013-08-08 15:23:11 -070027 .. method:: encrypt(plaintext)
28
29 :param bytes plaintext: The text you wish to encrypt.
30 :return bytes: Returns the ciphertext that was added.
31
32 .. method:: finalize()
33
34 :return bytes: Returns the remainder of the ciphertext.
Alex Gaynord96d1002013-08-08 07:37:26 -070035
36Ciphers
37~~~~~~~
38
Alex Gaynor4dd1c272013-08-08 11:39:21 -070039.. class:: cryptography.primitives.block.cipher.AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070040
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070041 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070042 AES is both fast, and cryptographically strong. It is a good default
43 choice for encryption.
44
45 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070046 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070047
Alex Gaynord96d1002013-08-08 07:37:26 -070048
49Modes
50~~~~~
51
Alex Gaynor4dd1c272013-08-08 11:39:21 -070052.. class:: cryptography.primitives.block.mode.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070053
54 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
55 considered cryptographically strong.
56
57 :param bytes initialization_vector: Must be random bytes. They do not need
58 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -070059 in a transmitted message). Must be the
60 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -070061 ``block_size`` of the cipher. Do not
62 reuse an ``initialization_vector`` with
63 a given ``key``.