[pycrypto] ARC4 examples

Legrandin helderijs at gmail.com
Wed Nov 20 13:19:33 PST 2013


>>>> from Crypto.Cipher import ARC4
>>>> from Crypto.Hash import SHA
>>>> from Crypto import Random
>>>>
>>>> key = b'Very long and confidential key'
>>>> nonce = Random.new().read(16)
>>>> tempkey = SHA.new(key+nonce).digest()
>>>> cipher = ARC4.new(tempkey)
>>>> msg = nonce + cipher.encrypt(b'Open the pod bay doors, HAL')
>
> I cannot get that to decrypt to the plaintext.
> What is the point of adding the nonce to the ciphertext please, and
> how to decrypt?

It is just one of the many possible ways for delivering the nonce (or
IV) to the receiver.

If you receive msg, and you have the key, you can decrypt using these steps:

from Crypto.Cipher import ARC4
from Crypto.Hash import SHA

nonce = msg[:16]
tempkey = SHA.new(key+nonce).digest()
cipher = ARC4.new(tempkey)
plaintext = cipher.decrypt(msg[16:])


More information about the pycrypto mailing list