[pycrypto] ARC4 examples

Dave Pawson dave.pawson at gmail.com
Sat Nov 23 01:40:12 PST 2013


On 20 November 2013 21:19, Legrandin <helderijs at gmail.com> wrote:
>>>>> 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.

But what is it please - what purpose does it serve?



>
> 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:])

Since I input text, I'm hoping to get text out?

Using this code I'm seeing
plaintext = str(cipher.decrypt(msg[:16]),'utf-8')
which gives
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd7 in position
1: invalid continuation byte

under python 3.

regards




-- 
Dave Pawson
XSLT XSL-FO FAQ.
Docbook FAQ.
http://www.dpawson.co.uk


More information about the pycrypto mailing list