ARC4.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/ARC4.py : ARC4
  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. """ARC4 symmetric cipher
  23. ARC4_ (Alleged RC4) is an implementation of RC4 (Rivest's Cipher version 4),
  24. a symmetric stream cipher designed by Ron Rivest in 1987.
  25. The cipher started as a proprietary design, that was reverse engineered and
  26. anonymously posted on Usenet in 1994. The company that owns RC4 (RSA Data
  27. Inc.) never confirmed the correctness of the leaked algorithm.
  28. Unlike RC2, the company has never published the full specification of RC4,
  29. of whom it still holds the trademark.
  30. ARC4 keys can vary in length from 40 to 2048 bits.
  31. One problem of ARC4 is that it does not take a nonce or an IV. If it is required
  32. to encrypt multiple messages with the same long-term key, a distinct
  33. independent nonce must be created for each message, and a short-term key must
  34. be derived from the combination of the long-term key and the nonce.
  35. Due to the weak key scheduling algorithm of RC2, the combination must be carried
  36. out with a complex function (e.g. a cryptographic hash) and not by simply
  37. concatenating key and nonce.
  38. New designs should not use ARC4. A good alternative is AES
  39. (`Crypto.Cipher.AES`) in any of the modes that turn it into a stream cipher (OFB, CFB, or CTR).
  40. As an example, encryption can be done as follows:
  41. >>> from Crypto.Cipher import ARC4
  42. >>> from Crypto.Hash import SHA
  43. >>> from Crypto import Random
  44. >>>
  45. >>> key = b'Very long and confidential key'
  46. >>> nonce = Random.new().read(16)
  47. >>> tempkey = SHA.new(key+nonce).digest()
  48. >>> cipher = ARC4.new(tempkey)
  49. >>> msg = nonce + cipher.encrypt(b'Open the pod bay doors, HAL')
  50. .. _ARC4: http://en.wikipedia.org/wiki/RC4
  51. :undocumented: __revision__, __package__
  52. """
  53. __revision__ = "$Id$"
  54. from Crypto.Cipher import _ARC4
  55. class ARC4Cipher:
  56. """ARC4 cipher object"""
  57. def __init__(self, key, *args, **kwargs):
  58. """Initialize an ARC4 cipher object
  59. See also `new()` at the module level."""
  60. self._cipher = _ARC4.new(key, *args, **kwargs)
  61. self.block_size = self._cipher.block_size
  62. self.key_size = self._cipher.key_size
  63. def encrypt(self, plaintext):
  64. """Encrypt a piece of data.
  65. :Parameters:
  66. plaintext : byte string
  67. The piece of data to encrypt. It can be of any size.
  68. :Return: the encrypted data (byte string, as long as the
  69. plaintext).
  70. """
  71. return self._cipher.encrypt(plaintext)
  72. def decrypt(self, ciphertext):
  73. """Decrypt a piece of data.
  74. :Parameters:
  75. ciphertext : byte string
  76. The piece of data to decrypt. It can be of any size.
  77. :Return: the decrypted data (byte string, as long as the
  78. ciphertext).
  79. """
  80. return self._cipher.decrypt(ciphertext)
  81. def new(key, *args, **kwargs):
  82. """Create a new ARC4 cipher
  83. :Parameters:
  84. key : byte string
  85. The secret key to use in the symmetric cipher.
  86. It can have any length, with a minimum of 40 bytes.
  87. Its cryptograpic strength is always capped to 2048 bits (256 bytes).
  88. :Return: an `ARC4Cipher` object
  89. """
  90. return ARC4Cipher(key, *args, **kwargs)
  91. #: Size of a data block (in bytes)
  92. block_size = 1
  93. #: Size of a key (in bytes)
  94. key_size = xrange(1,256+1)