~ubuntu-branches/ubuntu/saucy/python-crypto/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/Crypto/Cipher/XOR.py

  • Committer: Package Import Robot
  • Author(s): Sebastian Ramacher
  • Date: 2012-05-24 20:16:34 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120524201634-de6vxznjsdgekuwp
Tags: 2.6-1
* New upstream release.
  - Fixes CVE-2012-2417: insecure ElGamal key generation.
* Set urgency to high since this fixes a security issue.
* debian/copyright:
  - Fix formatting.
  - Update Format URL.
* debian/control:
  - Bump Standards-Version to 3.9.3 (no changes required).
  - Drop qNEW from Description since qNEW has been removed.
* debian/patches: Remove posixread.patch (not needed anymore).

Show diffs side-by-side

added added

removed removed

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