469,927 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Pycrypto

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

Jun 16 '06 #1
13 11115
luca72 wrote:
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%\x b9'
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.

Jun 16 '06 #2
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%\x b9'
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
Jun 16 '06 #3

Laszlo Nagy wrote:
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%\x b9'
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


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

Jun 16 '06 #4

Thanks

Luca

Jun 16 '06 #5
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
Jun 16 '06 #6

Excuse me again,
If the string is not a sting but hex number how i have to proced :

look this page:
http://www.cs.eku.edu/faculty/styer/...pt/JS-AES.html

Regards Luca

Jun 16 '06 #7
Hi,

Look at the bin2ascii module.

Philippe
luca72 wrote:

Excuse me again,
If the string is not a sting but hex number how i have to proced :

look this page:
http://www.cs.eku.edu/faculty/styer/...pt/JS-AES.html

Regards Luca


Jun 16 '06 #8

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',1 6))+chr(int('32',16))+chr(int('45',16))+chr(int('5 f',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

Jun 16 '06 #9
In <11**********************@h76g2000cwa.googlegroups .com>, luca72 wrote:
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',1 6))+chr(int('32',16))+chr(int('45',16))+chr(int('5 f',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('ea523a664dabaa4476d31226a1e3b ab0')
Out[27]:'\xeaR:fM\xab\xaaDv\xd3\x12&\xa1\xe3\xba\xb0'

Ciao,
Marc 'BlackJack' Rintsch
Jun 16 '06 #10
Laszlo Nagy wrote:
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


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_c...e_of_operation

James

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

http://www.jamesstroud.com/
Jun 16 '06 #11

In [26]:import binascii

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

Ciao,
Marc 'BlackJack' Rintsch


Ciao Marc

Grazie Thanks

Jun 16 '06 #12
wi******@hotmail.com wrote:
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+QwNrkPL3Ha0 8rLrb7PgCfUtBj
/V9NX16Jw01FvOSJYCUWZuA=
=Q6hN
-----END PGP SIGNATURE-----

Jun 17 '06 #13
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

Jun 26 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Haim Ashkenazi | last post: by
2 posts views Thread by Carmine Noviello | last post: by
11 posts views Thread by dirvine | last post: by
2 posts views Thread by Mike Meng | last post: by
2 posts views Thread by Ning | last post: by
3 posts views Thread by mirandacascade | last post: by
5 posts views Thread by mirandacascade | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.