PyCrypto AES MODE_CBC - How to?

Helmut Jarausch jarausch at igpm.rwth-aachen.de
Wed Feb 25 07:25:13 EST 2009


Helmut Jarausch wrote:
> Hi,
> 
> I've just tried to write a simple example using PyCrypto's
> AES (CBC mode)
> 
> #!/usr/bin/python
> from Crypto.Cipher import AES
> 
> PWD='abcdefghijklmnop'
> Initial16bytes='0123456789ABCDEF'
> 
> crypt = AES.new(PWD, AES.MODE_CBC,Initial16bytes)
> # crypt = AES.new(PWD, AES.MODE_ECB)
> 
> txt = 'ea523a664dabaa4476d31226a1e3bab0'
> 
> c = crypt.encrypt(txt)
> 
> txt_plain=crypt.decrypt(c)
> 
> print txt_plain
> 
> Unfortunately, txt_plain differs from txt - why?
> (Using MODE_ECB does work however)
> 

I just discovered that the following variant seems to work
crypt = AES.new(PWD, AES.MODE_CBC,Initial16bytes)
c = crypt.encrypt(txt)
crypt = AES.new(PWD, AES.MODE_CBC,Initial16bytes) # <<< re-initialize
txt_plain=crypt.decrypt(c)

So, the crypt object seems to keep some state.
I haven't seen this mentioned in the documentation.

Helmut.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany



More information about the Python-list mailing list