| 1 |
ARC4 |
| 2 |
==== |
| 3 |
|
| 4 |
.. warning:: |
| 5 |
ARC4 is not secure. |
| 6 |
Use :doc:`chacha20_poly1305` or :doc:`aes` (AEX or GCM mode) instead. |
| 7 |
This module is provided only for legacy purposes. |
| 8 |
|
| 9 |
ARC4_ (Alleged RC4) is an implementation of RC4 (Rivest's Cipher version 4), |
| 10 |
a symmetric stream cipher designed by Ron Rivest in 1987. |
| 11 |
|
| 12 |
The cipher started as a proprietary design, that was reverse engineered and |
| 13 |
anonymously posted on Usenet in 1994. The company that owns RC4 (RSA Data |
| 14 |
Inc.) never confirmed the correctness of the leaked algorithm. |
| 15 |
|
| 16 |
Unlike RC2, the company has never published the full specification of RC4, |
| 17 |
of which it still holds the trademark. |
| 18 |
|
| 19 |
ARC4 keys can vary in length from 8 to 2048 bits. |
| 20 |
|
| 21 |
One problem of ARC4 is that it does not take a nonce or an IV. |
| 22 |
If it is required to encrypt multiple messages with the same long-term key, a |
| 23 |
distinct independent nonce must be created for each message, and a short-term |
| 24 |
key must be derived from the combination of the long-term key and the nonce. |
| 25 |
Due to the weak key scheduling algorithm of ARC4, the combination must be |
| 26 |
carried out with a complex function (e.g. a cryptographic hash) and not by |
| 27 |
simply concatenating key and nonce. |
| 28 |
|
| 29 |
As an example, encryption can be done as follows:: |
| 30 |
|
| 31 |
>>> from Crypto.Cipher import ARC4 |
| 32 |
>>> from Crypto.Hash import SHA256, HMAC |
| 33 |
>>> from Crypto.Random import get_random_bytes |
| 34 |
>>> |
| 35 |
>>> key = b'Very long and confidential key' |
| 36 |
>>> nonce = get_random_bytes(16) |
| 37 |
>>> tempkey = HMAC.new(key, nonce, digestmod=SHA256).digest() |
| 38 |
>>> cipher = ARC4.new(tempkey) |
| 39 |
>>> msg = nonce + cipher.encrypt(b'Open the pod bay doors, HAL') |
| 40 |
|
| 41 |
.. _ARC4: http://en.wikipedia.org/wiki/RC4 |
| 42 |
|
| 43 |
.. automodule:: Crypto.Cipher.ARC4 |
| 44 |
:members: |