XOR.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/XOR.py : XOR
  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. """XOR toy cipher
  23. XOR is one the simplest stream ciphers. Encryption and decryption are
  24. performed by XOR-ing data with a keystream made by contatenating
  25. the key.
  26. Do not use it for real applications!
  27. :undocumented: __revision__, __package__
  28. """
  29. __revision__ = "$Id$"
  30. from Crypto.Cipher import _XOR
  31. class XORCipher:
  32. """XOR cipher object"""
  33. def __init__(self, key, *args, **kwargs):
  34. """Initialize a XOR cipher object
  35. See also `new()` at the module level."""
  36. self._cipher = _XOR.new(key, *args, **kwargs)
  37. self.block_size = self._cipher.block_size
  38. self.key_size = self._cipher.key_size
  39. def encrypt(self, plaintext):
  40. """Encrypt a piece of data.
  41. :Parameters:
  42. plaintext : byte string
  43. The piece of data to encrypt. It can be of any size.
  44. :Return: the encrypted data (byte string, as long as the
  45. plaintext).
  46. """
  47. return self._cipher.encrypt(plaintext)
  48. def decrypt(self, ciphertext):
  49. """Decrypt a piece of data.
  50. :Parameters:
  51. ciphertext : byte string
  52. The piece of data to decrypt. It can be of any size.
  53. :Return: the decrypted data (byte string, as long as the
  54. ciphertext).
  55. """
  56. return self._cipher.decrypt(ciphertext)
  57. def new(key, *args, **kwargs):
  58. """Create a new XOR cipher
  59. :Parameters:
  60. key : byte string
  61. The secret key to use in the symmetric cipher.
  62. Its length may vary from 1 to 32 bytes.
  63. :Return: an `XORCipher` object
  64. """
  65. return XORCipher(key, *args, **kwargs)
  66. #: Size of a data block (in bytes)
  67. block_size = 1
  68. #: Size of a key (in bytes)
  69. key_size = xrange(1,32+1)