Pycrypto

L

luca72

Hello
I have to make an easy operation but reading the pycrypto doc. a never
see AES example
I have to cript this key 'ea523a664dabaa4476d31226a1e3bab0' with the
AES.
Can you help me for make it with pycrypto

Regards Luca
 
W

wittempj

luca72 said:
Hello
I have to make an easy operation but reading the pycrypto doc. a never
see AES example
I have to cript this key 'ea523a664dabaa4476d31226a1e3bab0' with the
AES.
Can you help me for make it with pycrypto

Regards Luca

You can do this as follows:

py> from Crypto.Cipher import AES
py> # key has to be 16, 24 or 32 bytes for AES
py> crypt = AES.new('abcdefghijklmnop', AES.MODE_ECB)
# we're lucky, the string to encrypt is a multiple of 16 in length
py> txt = 'ea523a664dabaa4476d31226a1e3bab0'
py> c = crypt.encrypt(txt)
py> c
'w\x81\xe3\xdd\x066\x9eY\xc7\xce~O\x9e\xfb\xef\xfa\xb5\x8a\xac\x7f\xca\x9fl{\xe5\xfd6\x80\xe3\x81%\xb9'
py> crypt.decrypt(c)
'ea523a664dabaa4476d31226a1e3bab0'

see http://www.amk.ca/python/writing/pycrypt for the docs. if you have
to encrypt data which has not a multiple of length 16 you have to pad
it e.g. with spaces, and then strip the decrypt() result.
 
L

Laszlo Nagy

You can do this as follows:

py> from Crypto.Cipher import AES
py> # key has to be 16, 24 or 32 bytes for AES
py> crypt = AES.new('abcdefghijklmnop', AES.MODE_ECB)
# we're lucky, the string to encrypt is a multiple of 16 in length
py> txt = 'ea523a664dabaa4476d31226a1e3bab0'
py> c = crypt.encrypt(txt)
py> c
'w\x81\xe3\xdd\x066\x9eY\xc7\xce~O\x9e\xfb\xef\xfa\xb5\x8a\xac\x7f\xca\x9fl{\xe5\xfd6\x80\xe3\x81%\xb9'
py> crypt.decrypt(c)
'ea523a664dabaa4476d31226a1e3bab0'

see http://www.amk.ca/python/writing/pycrypt for the docs. if you have
to encrypt data which has not a multiple of length 16 you have to pad
it e.g. with spaces, and then strip the decrypt() result.
Or use CBC mode? I'm not familiar with pycrypto but I know that CBC mode
can crypt/decrypt text with any size.

Laszlo
 
W

wittempj

Laszlo said:
Or use CBC mode? I'm not familiar with pycrypto but I know that CBC mode
can crypt/decrypt text with any size.

Laszlo

Not in this implementation:
py> from Crypto.Cipher import AES
py> crypt = AES.new('abcdefghijklmnop', AES.MODE_CBC)
py> c = crypt.encrypt('1')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: Input strings must be a multiple of 16 in length
 
L

Laszlo Nagy

Not in this implementation:
py> from Crypto.Cipher import AES
py> crypt = AES.new('abcdefghijklmnop', AES.MODE_CBC)
py> c = crypt.encrypt('1')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: Input strings must be a multiple of 16 in length
This is strange. In theory, any ECB mode cipher can be used to create a
CBC mode cipher.
AFAIK, CBC creates one encrypted block, and uses the one byte from the
plain text to xor it
with the last encrypted byte. Finally it shifts the encrypted block.
This way each input byte will
have a corresponding output byte, and there is no size limit for the
plain text.

Frankly, I could write the CBC mode cipher using the (already existing)
ECB cipher. Why we have this limitation?

Laszlo
 
L

luca72

Hello again i have solve doing this:

from Crypto.Cipher import AES
stri=(chr(int('9b',16))+chr(int('d3',16))+chr(int('2d',16))+chr(int('24',16))+chr(int('af',16))+chr(int('c9',16))+chr(int('e9',16))+chr(int('d7',16))+chr(int('46',16))+chr(int('69',16))+chr(int('71',16))+chr(int('32',16))+chr(int('45',16))+chr(int('5f',16))+chr(int('27',16))+chr(int('0b',16)))
luca = str(stri)
crypt = AES.new(luca, AES.MODE_ECB)
testo=(chr(int('ea',16))+chr(int('52',16))+chr(int('3a',16))+chr(int('66',16))+chr(int('4d',16))+chr(int('ab',16))+chr(int('aa',16))+chr(int('44',16))+chr(int('76',16))+chr(int('d3',16))+chr(int('12',16))+chr(int('26',16))+chr(int('a1',16))+chr(int('e3',16))+chr(int('ba',16))+chr(int('b0',16)))
testo = str(testo)
c = crypt.encrypt(testo)

I don't know if this is the best way , but anyway it work

Regards

Luca
 
M

Marc 'BlackJack' Rintsch

Hello again i have solve doing this:

from Crypto.Cipher import AES
stri=(chr(int('9b',16))+chr(int('d3',16))+chr(int('2d',16))+chr(int('24',16))+chr(int('af',16))+chr(int('c9',16))+chr(int('e9',16))+chr(int('d7',16))+chr(int('46',16))+chr(int('69',16))+chr(int('71',16))+chr(int('32',16))+chr(int('45',16))+chr(int('5f',16))+chr(int('27',16))+chr(int('0b',16)))
luca = str(stri)
crypt = AES.new(luca, AES.MODE_ECB)
testo=(chr(int('ea',16))+chr(int('52',16))+chr(int('3a',16))+chr(int('66',16))+chr(int('4d',16))+chr(int('ab',16))+chr(int('aa',16))+chr(int('44',16))+chr(int('76',16))+chr(int('d3',16))+chr(int('12',16))+chr(int('26',16))+chr(int('a1',16))+chr(int('e3',16))+chr(int('ba',16))+chr(int('b0',16)))
testo = str(testo)
c = crypt.encrypt(testo)

I don't know if this is the best way , but anyway it work

In [26]:import binascii

In [27]:binascii.unhexlify('ea523a664dabaa4476d31226a1e3bab0')
Out[27]:'\xeaR:fM\xab\xaaDv\xd3\x12&\xa1\xe3\xba\xb0'

Ciao,
Marc 'BlackJack' Rintsch
 
J

James Stroud

Laszlo said:
This is strange. In theory, any ECB mode cipher can be used to create a
CBC mode cipher.
AFAIK, CBC creates one encrypted block, and uses the one byte from the
plain text to xor it
with the last encrypted byte. Finally it shifts the encrypted block.
This way each input byte will
have a corresponding output byte, and there is no size limit for the
plain text.

Frankly, I could write the CBC mode cipher using the (already existing)
ECB cipher. Why we have this limitation?

Laszlo

CBC mode is cipher block chaining, so it still works as a block cipher,
which means that it must be that len(text) % block_size == 0. In other
words, CBC does not shift by one byte but by block_size bytes. See:

http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
L

luca72

In [26]:import binascii

In [27]:binascii.unhexlify('ea523a664dabaa4476d31226a1e3bab0')
Out[27]:'\xeaR:fM\xab\xaaDv\xd3\x12&\xa1\xe3\xba\xb0'

Ciao,
Marc 'BlackJack' Rintsch

Ciao Marc

Grazie Thanks
 
K

K.S.Sreeram

ValueError: Input strings must be a multiple of 16 in length

As James Stroud noted, a CBC mode cipher is still a block cipher, and
the input *must* be a multiple of the block size.

OpenSSL provides a standard padding mechanism so that there are no input
size limitations for any cipher.

Have a look at http://tachyon.in/ncrypt/
It provides access to the OpenSSL ciphers (including padding).

Regards
Sreeram


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFElAdqrgn0plK5qqURAuN3AKCIXvFZAf+QwNrkPL3Ha08rLrb7PgCfUtBj
/V9NX16Jw01FvOSJYCUWZuA=
=Q6hN
-----END PGP SIGNATURE-----
 
L

luca72

Hello again

You know if is possible save all the encryption process in a text file
and not only the result?

This will be wery helpful for compare the pycrypto step by step
operation, with the hand made operation and see where hand made make a
mistake
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top