PyCrypto AES MODE_CBC - How to?

H

Helmut Jarausch

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)

What am I missing?

Many thanks for a hint,

Helmut Jarausch

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

Helmut Jarausch

Helmut said:
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
 
M

M.-A. Lemburg

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.

In CBC mode, all previous encryptions affect the next one,
since the blocks are chained:

http://en.wikipedia.org/wiki/Cipher_block_chaining#Cipher-block_chaining_.28CBC.29

In ECB mode, they are not, but then it doesn't provide much
security unless you change the key frequently:

http://en.wikipedia.org/wiki/Cipher_block_chaining#Electronic_codebook_.28ECB.29

(the wiki pages provide some nice graphics to see what's going
on and why CBC is better than ECB)

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Feb 26 2009)________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top