OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # Cipher/DES.py : DES |
| 4 # |
| 5 # =================================================================== |
| 6 # The contents of this file are dedicated to the public domain. To |
| 7 # the extent that dedication to the public domain is not available, |
| 8 # everyone is granted a worldwide, perpetual, royalty-free, |
| 9 # non-exclusive license to exercise all rights associated with the |
| 10 # contents of this file for any purpose whatsoever. |
| 11 # No rights are reserved. |
| 12 # |
| 13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 14 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 15 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 16 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 17 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 18 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 # SOFTWARE. |
| 21 # =================================================================== |
| 22 """DES symmetric cipher |
| 23 |
| 24 DES `(Data Encryption Standard)`__ is a symmetric block cipher standardized |
| 25 by NIST_ . It has a fixed data block size of 8 bytes. |
| 26 Its keys are 64 bits long, even though 8 bits were used for integrity (now they |
| 27 are ignored) and do not contribute to securty. |
| 28 |
| 29 DES is cryptographically secure, but its key length is too short by nowadays |
| 30 standards and it could be brute forced with some effort. |
| 31 |
| 32 DES should not be used for new designs. Use `AES`. |
| 33 |
| 34 As an example, encryption can be done as follows: |
| 35 |
| 36 >>> from Crypto.Cipher import DES3 |
| 37 >>> from Crypto import Random |
| 38 >>> |
| 39 >>> key = b'Sixteen byte key' |
| 40 >>> iv = Random.new().read(DES3.block_size) |
| 41 >>> cipher = DES3.new(key, DES3.MODE_OFB, iv) |
| 42 >>> plaintext = b'sona si latine loqueris ' |
| 43 >>> msg = iv + cipher.encrypt(plaintext) |
| 44 |
| 45 .. __: http://en.wikipedia.org/wiki/Data_Encryption_Standard |
| 46 .. _NIST: http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf |
| 47 |
| 48 :undocumented: __revision__, __package__ |
| 49 """ |
| 50 |
| 51 __revision__ = "$Id$" |
| 52 |
| 53 from Crypto.Cipher import blockalgo |
| 54 from Crypto.Cipher import _DES |
| 55 |
| 56 class DESCipher(blockalgo.BlockAlgo): |
| 57 """DES cipher object""" |
| 58 |
| 59 def __init__(self, key, *args, **kwargs): |
| 60 """Initialize a DES cipher object |
| 61 |
| 62 See also `new()` at the module level.""" |
| 63 blockalgo.BlockAlgo.__init__(self, _DES, key, *args, **kwargs) |
| 64 |
| 65 def new(key, *args, **kwargs): |
| 66 """Create a new DES cipher |
| 67 |
| 68 :Parameters: |
| 69 key : byte string |
| 70 The secret key to use in the symmetric cipher. |
| 71 It must be 8 byte long. The parity bits will be ignored. |
| 72 :Keywords: |
| 73 mode : a *MODE_** constant |
| 74 The chaining mode to use for encryption or decryption. |
| 75 Default is `MODE_ECB`. |
| 76 IV : byte string |
| 77 The initialization vector to use for encryption or decryption. |
| 78 |
| 79 It is ignored for `MODE_ECB` and `MODE_CTR`. |
| 80 |
| 81 For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption |
| 82 and `block_size` +2 bytes for decryption (in the latter case, it is |
| 83 actually the *encrypted* IV which was prefixed to the ciphertext). |
| 84 It is mandatory. |
| 85 |
| 86 For all other modes, it must be `block_size` bytes longs. It is optional
and |
| 87 when not present it will be given a default value of all zeroes. |
| 88 counter : callable |
| 89 (*Only* `MODE_CTR`). A stateful function that returns the next |
| 90 *counter block*, which is a byte string of `block_size` bytes. |
| 91 For better performance, use `Crypto.Util.Counter`. |
| 92 segment_size : integer |
| 93 (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext |
| 94 are segmented in. |
| 95 It must be a multiple of 8. If 0 or not specified, it will be assumed to
be 8. |
| 96 |
| 97 :Return: an `DESCipher` object |
| 98 """ |
| 99 return DESCipher(key, *args, **kwargs) |
| 100 |
| 101 #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`. |
| 102 MODE_ECB = 1 |
| 103 #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`. |
| 104 MODE_CBC = 2 |
| 105 #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`. |
| 106 MODE_CFB = 3 |
| 107 #: This mode should not be used. |
| 108 MODE_PGP = 4 |
| 109 #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`. |
| 110 MODE_OFB = 5 |
| 111 #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`. |
| 112 MODE_CTR = 6 |
| 113 #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`. |
| 114 MODE_OPENPGP = 7 |
| 115 #: Size of a data block (in bytes) |
| 116 block_size = 8 |
| 117 #: Size of a key (in bytes) |
| 118 key_size = 8 |
OLD | NEW |