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