How to generate strong one time session key for AES in python

20,005

Solution 1

AES-128 has 128 bit key = 16 bytes.

random_key = os.urandom(16)

should be sufficient for most uses. When you feed this random value to M2 (or whatever crypto library), it is transformed internally into a "key schedule" actually used for encryption.

Solution 2

M2Crypto is notorious for lack of good documentation.

Here is what I could gather from their test cases:

import os
from M2Crypto import EVP

k = EVP.Cipher(alg='aes_128_cbc', key=os.urandom(16), iv=os.urandom(16), op=enc)
Share:
20,005
Admin
Author by

Admin

Updated on July 19, 2022

Comments

  • Admin
    Admin almost 2 years

    I am using M2Crypto's AES for encrypting message, but confused about how to generate a strong random session key and of what length. Does M2Crypto provide any function for generation random key.