I want to generate a 16 bytes long random string for encryption purpose (AES encryption) in python3. urandom(n)
seems to be the way to go to get real random characters:
os.urandom(n):
Return a string of n random bytes suitable for cryptographic use.
As I need a 16 bytes random string, I thought this would do the job for me: EDIT I included now a more complex example, demonstrating the issues I have.
from os import urandom
from Crypto.Cipher import AES
from base64 import b64encode
import sys
rnd=urandom(16)
rnd_bytes=b64encode(rnd).decode('utf-8')
print(sys.getsizeof(rnd_bytes))
print(len(rnd_bytes))
print(type(rnd_bytes))
AESencrypter=AES.new('my key',AES.MODE_CBC,rnd_bytes)
Note: I used this answer for converting the urandom byte to a string. The output is:
73
24
File "/User/test.py", line 14, in AESencrypter=AES.new('my kes',AES.MODE_CBC,rnd_bytes) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Crypto/Cipher/AES.py", line 95, in new return AESCipher(key, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Crypto/Cipher/AES.py", line 59, in init blockalgo.BlockAlgo.init(self, _AES, key, *args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Crypto/Cipher/blockalgo.py", line 141, in init self._cipher = factory.new(key, *args, **kwargs) ValueError: IV must be 16 bytes long
As you see, the length is not 16 (probably because of the conversion between byte and string). How can I fix that?