AES256 in PyCrypto

Discussion in 'Python' started by mirandacascade@yahoo.com, Jan 7, 2007.

  1. Guest

    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.
     
    , Jan 7, 2007
    #1
    1. Advertisements

  2. In <>, mirandacascade
    wrote:

    > 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
     
    Marc 'BlackJack' Rintsch, Jan 7, 2007
    #2
    1. Advertisements

  3. Guest

    Marc 'BlackJack' Rintsch wrote:

    > `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.
     
    , Jan 7, 2007
    #3
  4. [ <> ]

    >
    > Marc 'BlackJack' Rintsch wrote:
    >
    >> `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...

    --
    Freedom is always the freedom of dissenters.
    (Rosa Luxemburg)
     
    Sebastian 'lunar' Wiesner, Jan 7, 2007
    #4
  5. Guest

    Sebastian 'lunar' Wiesner wrote:

    > 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:
    >>> from Crypto.Cipher import AES
    >>> x = AES.new(

    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...
    >>> x.encrypt(

    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.
     
    , Jan 7, 2007
    #5
  6. At Sunday 7/1/2007 18:23, wrote:

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

    > >>> x = AES.new(

    >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
     
    Gabriel Genellina, Jan 8, 2007
    #6
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Haim Ashkenazi

    compile pycrypto on windows

    Haim Ashkenazi, Feb 10, 2004, in forum: Python
    Replies:
    2
    Views:
    878
    Haim Ashkenazi
    Feb 11, 2004
  2. Carmine Noviello

    PyCrypto and RSA

    Carmine Noviello, Feb 28, 2004, in forum: Python
    Replies:
    2
    Views:
    1,465
    Carmine Noviello
    Mar 1, 2004
  3. Fuzzyman

    ANN: PyCrypto 2.0 Binary Installer

    Fuzzyman, Dec 13, 2004, in forum: Python
    Replies:
    0
    Views:
    528
    Fuzzyman
    Dec 13, 2004
  4. Fuzzyman
    Replies:
    0
    Views:
    447
    Fuzzyman
    Jan 12, 2005
  5. Stone
    Replies:
    6
    Views:
    2,535
    Roedy Green
    Jun 19, 2011
Loading...

Share This Page