[pycrypto] ARC4 examples

Dave Pawson dave.pawson at gmail.com
Sat Nov 23 03:28:29 PST 2013


Thanks for that Legrandin,
Once I got my head round it...

For compleness

        #
        # Test again, bearing in mind warning at
        # https://www.dlitz.net/software/pycrypto/api/current/Crypto.Cipher.ARC4-module.html
        # Need to deal in byte strings
    def test_ARC4_2(self):
        key = b'Very long and confidential key'
        nonce = Random.new().read(16)
        tempkey = SHA.new(key+nonce).digest()

        cipher = ARC4.new(tempkey)
        ciphertext = nonce + cipher.encrypt(b'Open the pod bay doors, HAL')

        # decrypt with new cipher
        nonce = ciphertext[:16] # strip off nonce
        tempkey = SHA.new(key+nonce).digest()

        cipher = ARC4.new(tempkey)
        plaintext = cipher.decrypt(ciphertext[16:]) # Only decrypt the
msg after nonce.
        print(plaintext)
        self.assertEqual(b'Open the pod bay doors,
HAL',plaintext,'arc4,2 failed')

regard DaveP



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.
>
> 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:])
> _______________________________________________
> pycrypto mailing list
> pycrypto at lists.dlitz.net
> http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto



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


More information about the pycrypto mailing list