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.
Asked
Active
Viewed 2.7k times
3 Answers
19
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.

Dmitry Dvoinikov
- 411
- 3
- 3
4
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)

user7305
- 5,741
- 9
- 28
- 23
0
If you are encrypting to send to another party then you want to do something like Diffie Hellman or ECDH key exchange to establish a shared secret. If you just want to encrypt for storage, then you need a secure random number generator. I do not believe M2Crypto provides this?
It looks like M2Crypto does support Diffie Hellman.

James
- 24,676
- 13
- 84
- 130