DES.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. DES `(Data Encryption Standard)`__ is a symmetric block cipher standardized
  24. by NIST_ . It has a fixed data block size of 8 bytes.
  25. Its keys are 64 bits long, even though 8 bits were used for integrity (now they
  26. are ignored) and do not contribute to securty.
  27. DES is cryptographically secure, but its key length is too short by nowadays
  28. standards and it could be brute forced with some effort.
  29. DES should not be used for new designs. Use `AES`.
  30. As an example, encryption can be done as follows:
  31. >>> from Crypto.Cipher import DES3
  32. >>> from Crypto import Random
  33. >>>
  34. >>> key = b'Sixteen byte key'
  35. >>> iv = Random.new().read(DES3.block_size)
  36. >>> cipher = DES3.new(key, DES3.MODE_OFB, iv)
  37. >>> plaintext = b'sona si latine loqueris '
  38. >>> msg = iv + cipher.encrypt(plaintext)
  39. .. __: http://en.wikipedia.org/wiki/Data_Encryption_Standard
  40. .. _NIST: http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  41. :undocumented: __revision__, __package__
  42. """
  43. __revision__ = "$Id$"
  44. from Crypto.Cipher import blockalgo
  45. from Crypto.Cipher import _DES
  46. class DESCipher(blockalgo.BlockAlgo):
  47. """DES cipher object"""
  48. def __init__(self, key, *args, **kwargs):
  49. """Initialize a DES cipher object
  50. See also `new()` at the module level."""
  51. blockalgo.BlockAlgo.__init__(self, _DES, key, *args, **kwargs)
  52. def new(key, *args, **kwargs):
  53. """Create a new DES cipher
  54. :Parameters:
  55. key : byte string
  56. The secret key to use in the symmetric cipher.
  57. It must be 8 byte long. The parity bits will be ignored.
  58. :Keywords:
  59. mode : a *MODE_** constant
  60. The chaining mode to use for encryption or decryption.
  61. Default is `MODE_ECB`.
  62. IV : byte string
  63. The initialization vector to use for encryption or decryption.
  64. It is ignored for `MODE_ECB` and `MODE_CTR`.
  65. For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
  66. and `block_size` +2 bytes for decryption (in the latter case, it is
  67. actually the *encrypted* IV which was prefixed to the ciphertext).
  68. It is mandatory.
  69. For all other modes, it must be `block_size` bytes longs. It is optional and
  70. when not present it will be given a default value of all zeroes.
  71. counter : callable
  72. (*Only* `MODE_CTR`). A stateful function that returns the next
  73. *counter block*, which is a byte string of `block_size` bytes.
  74. For better performance, use `Crypto.Util.Counter`.
  75. segment_size : integer
  76. (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
  77. are segmented in.
  78. It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
  79. :Return: an `DESCipher` object
  80. """
  81. return DESCipher(key, *args, **kwargs)
  82. #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
  83. MODE_ECB = 1
  84. #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
  85. MODE_CBC = 2
  86. #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
  87. MODE_CFB = 3
  88. #: This mode should not be used.
  89. MODE_PGP = 4
  90. #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
  91. MODE_OFB = 5
  92. #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
  93. MODE_CTR = 6
  94. #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
  95. MODE_OPENPGP = 7
  96. #: Size of a data block (in bytes)
  97. block_size = 8
  98. #: Size of a key (in bytes)
  99. key_size = 8