blob: 7b67bee028f7e3ddcb12a3ea46be029f65a9298b [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 Gaynore62aa402013-08-08 15:23:11 -070024 .. method:: encrypt(plaintext)
25
26 :param bytes plaintext: The text you wish to encrypt.
27 :return bytes: Returns the ciphertext that was added.
28
29 .. method:: finalize()
30
31 :return bytes: Returns the remainder of the ciphertext.
Alex Gaynord96d1002013-08-08 07:37:26 -070032
33Ciphers
34~~~~~~~
35
Alex Gaynor4dd1c272013-08-08 11:39:21 -070036.. class:: cryptography.primitives.block.cipher.AES(key)
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070037
Alex Gaynor1e3f81f2013-08-08 11:31:43 -070038 AES (Advanced Encryption Standard) is a block cipher standardized by NIST.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070039 AES is both fast, and cryptographically strong. It is a good default
40 choice for encryption.
41
42 :param bytes key: The secret key, either ``128``, ``192``, or ``256`` bits.
Alex Gaynor48ec9a32013-08-08 11:13:46 -070043 This must be kept secret.
Alex Gaynor5ba2dfa2013-08-08 11:04:44 -070044
Alex Gaynord96d1002013-08-08 07:37:26 -070045
46Modes
47~~~~~
48
Alex Gaynor4dd1c272013-08-08 11:39:21 -070049.. class:: cryptography.primitives.block.mode.CBC(initialization_vector)
Alex Gaynor48ec9a32013-08-08 11:13:46 -070050
51 CBC (Cipher block chaining) is a mode of operation for block ciphers. It is
52 considered cryptographically strong.
53
54 :param bytes initialization_vector: Must be random bytes. They do not need
55 to be kept secret (they can be included
Alex Gaynor2dc2b862013-08-08 11:58:04 -070056 in a transmitted message). Must be the
57 same number of bytes as the
Alex Gaynor6badd9b2013-08-08 14:59:53 -070058 ``block_size`` of the cipher. Do not
59 reuse an ``initialization_vector`` with
60 a given ``key``.