AES256 in PyCrypto

M

mirandacascade

Attempting to determine whether the PyCrypto package has the capability
to perform AES256 encryption. I received the following C# snippet:

CryptoProvider provider = new CryptoProvider();
Encrypted_Type password = new Encrypted_Type();
password.EncryptedData = new EncryptedDataType();
password.EncryptedData.EncryptionMethod = new EncryptionMethodType();
password.EncryptedData.EncryptionMethod.Algorithm = "AES256-cbc";

and I was told that it was the setup code for code that later on
performs AES256 encryption. I'm assuming that setting the Algorithm
property is what informs the system as to the type of encryption to
perform. I included the above snippet as a reference point, because
I'm attempting to understand how to do something equivalent in Python.

Would the following Python code perform AES256 encryption on plainText
from Crypto.Cipher import AES
x = AES.new(a, AES.MODE_CBC, iv)
x.encrypt(plainText)

assuming:
a = the key value
iv = an initialization vector
?

If the above Python code does not perform AES256 encryption:
a) is there functionality within PyCrypto that allows one to perform
AES256 encryption?
b) if such functionality does not exist in PyCrypto, does it exist in
some other Python package?

Operating System: Windows XP
Vsn of Python: 2.4

Thank you.
 
M

Marc 'BlackJack' Rintsch

mirandacascade said:
Would the following Python code perform AES256 encryption on plainText
from Crypto.Cipher import AES
x = AES.new(a, AES.MODE_CBC, iv)
x.encrypt(plainText)

assuming:
a = the key value
iv = an initialization vector
?

`a` must be of length 32 for AES256. And the length of `plainText` must
be a multiple of 16 because it's a block cypher algorithm.

Ciao,
Marc 'BlackJack' Rintsch
 
M

mirandacascade

Marc said:
`a` must be of length 32 for AES256. And the length of `plainText` must
be a multiple of 16 because it's a block cypher algorithm.

Thank you. I have some follow up questions and 1 tangential question.

Follow up question:
Would it be correct to infer that:
a) the AES.pyd extension module (plus whatever additional files within
the PyCrypto package that it uses) has the capability to perform AES256
encryption?
b) the AES256 encryption happens based on the characteristics of the
input to the new() method...if the first argument has a length of 32,
the result will be AES256-style encryption?
c) will AES256-style encryption also happen if the first argument to
the new() method has a length that is a multiple of 32, e.g. 64?

Tangential question:
Is there functionality available (either in the PyCrypto package or
some other package) that generates an initialization vector that can be
used as input to the new() method? What prompts this question is that
the original posting referenced a snippet of C# code; some other
related snippets I saw seemed to suggest that:
a) a RijndaelManaged() class gets instantiated
b) that class has a GenerateIV() method which appears to populate
someting in a IV property
c) the application that was employing the AES256 encryption made use of
the left-most 16 characters of the IV property
So, I was curious whether something analgous exists in the Python
world.

Thank you.
 
S

Sebastian 'lunar' Wiesner

`a` must be of length 32 for AES256. And the length of `plainText`
must be a multiple of 16 because it's a block cypher algorithm.

Thank you. I have some follow up questions and 1 tangential question.

Follow up question:
Would it be correct to infer that:
a) the AES.pyd extension module (plus whatever additional files
within the PyCrypto package that it uses) has the capability to
perform AES256 encryption?
b) the AES256 encryption happens based on the characteristics of the
input to the new() method...if the first argument has a length of 32,
the result will be AES256-style encryption?

Since you are apparently unable to read to docstrings of this module, I
will give you a short hint: yes, pycrypto supports AES with 256 bit
keys.
c) will AES256-style encryption also happen if the first argument to
the new() method has a length that is a multiple of 32, e.g. 64?

Why didn't you try this? It would have answered your question:

[12]--> AES.new(os.urandom(64), AES.MODE_CBC, os.urandom(16))
---------------------------------------------------------------------------
exceptions.ValueError Traceback (most
recent call last)

/home/lunar/<ipython console>

ValueError: AES key must be either 16, 24, or 32 bytes long

Tangential question:
Is there functionality available (either in the PyCrypto package or
some other package) that generates an initialization vector that can
be used as input to the new() method? What prompts this question is
that the original posting referenced a snippet of C# code; some other
related snippets I saw seemed to suggest that:
a) a RijndaelManaged() class gets instantiated
b) that class has a GenerateIV() method which appears to populate
someting in a IV property
c) the application that was employing the AES256 encryption made use
of the left-most 16 characters of the IV property
So, I was curious whether something analgous exists in the Python
world.

os.urandom will be your friend...
 
M

mirandacascade

Sebastian said:
Since you are apparently unable to read to docstrings of this module, I
will give you a short hint: yes, pycrypto supports AES with 256 bit
keys.
Thank you for the information.

The material I consulted was:
a) the PyCrypto manual: http://www.amk.ca/python/writing/pycrypt/
b) the .py files that shipped with the PyCrypto package

Is a docstring is the text between the three consecutive quote
characters in a .py file? The reason for the question is that I looked
at the .py files that shipped with PyCrypto. Of the various .py files
that shipped with PyCrypto, there were two files (both __init__.py)
that contained information seemed to pertain to AES. The stuff between
the 3 consecutive quote chars in:
Crypto.__init__.py
Crypto.Cipher.__init__.py
make reference to AES, but I wasn't able to determine from what I read
if that was AES256.
Which .py file contain the docstrings that flesh out the information
summarized in the short hint?

Can docstrings be embedded within the .pyd extension modules as well?
Does the AES.pyd extension module have docstrings? How does one view
the docstrings in a .pyd file? When I reference AES from the
interactive of Pythonwin:As soon as I type the '(' character, the IDE displays:
new(key, [mode], [IV]): Return a new AES encryption object

then when x gets instantiated, and the encrypt method gets called...As soon as I type the '(' character, the IDE displays:
Encrypt the provided string of binary data

I'm guessing that what the IDE is displaying is the first line of what
may be multiple-line docstring that is embedded within the .pyd
extension module? Might there be more lines in the docstring?

Thank you.
 
G

Gabriel Genellina

At said:
Is a docstring is the text between the three consecutive quote
characters in a .py file? The reason for the question is that I looked

See section 4.6 in the Python Tutorial - I strongly suggest you read
it (or any other introductory text like diveintopython)
Can docstrings be embedded within the .pyd extension modules as well?

Yes - if the original module writer has provided it.
As soon as I type the '(' character, the IDE displays:
new(key, [mode], [IV]): Return a new AES encryption object [...]
I'm guessing that what the IDE is displaying is the first line of what
may be multiple-line docstring that is embedded within the .pyd
extension module? Might there be more lines in the docstring?

I don't know which IDE are you using, but try typing "help(AES.new)"
or simply "help" (without quotes) in the interpreter.


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top