# =========================================================
# Generate AES 256 (bit) keys
# =========================================================
import os
import sys
# ---------------------------------------------------------
# running Python 3?
# ---------------------------------------------------------
def RunningPython3():
if sys.version_info[0] == 3:
return True
return False
# ---------------------------------------------------------
# get user imput
# ---------------------------------------------------------
def GetUserInput(prompt,py3):
if py3:
return input(prompt)
else:
return raw_input(prompt)
# ---------------------------------------------------------
#
# ---------------------------------------------------------
def strhex(s):
def printable(c):
if c < 0x20:
return False
if c > 0x7f:
return False
return True
def chrhex(c):
#print('({}) (len={}) ({})'.format(c,len(c),type(ord(c))))
if printable(c):
return c
cc = ord(c)
if cc <= 0xff:
return r'\x{0:02x}'.format(cc)
elif cc <= '\uffff':
return r'\u{0:04x}'.format(cc)
else:
return r'\U{0:08x}'.format(cc)
return ''.join(chrhex(c) for c in s)
# ---------------------------------------------------------
# main
# ---------------------------------------------------------
print('''
+--------------------------------------+
+ Generate AES 256 bit random Keys +
+--------------------------------------+''')
py3 = RunningPython3()
while True:
yn = GetUserInput('\nGenerate AES Key y/n [n]: ', py3)
yn = yn.strip()
if yn == '':
break
if yn[0] == 'y' or yn[0] == 'Y':
key = os.urandom(32)
print('')
if py3:
print('key =', key)
else:
key = 'b\'' + strhex(key) + '\''
print('Key = ' + key)
print('key type = ' + str(type(key)))
print('Key length = ' + str(len(key)))
else:
break
print('\n')