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

user553947 picture user553947 · Feb 15, 2011 · Viewed 17.6k times · Source

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.

Answer

Dmitry Dvoinikov picture Dmitry Dvoinikov · Feb 15, 2011

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.