I'm using the pycryptodome
module and its AES functionality to encrypt some data. However I need to generate a key for the AEScipher
that I can later retrieve. The project stores all private data (including keys) in the form of an image. Basically we use an array of pixels and create the image using PIL
and retrieve the pixel_list using the getdata()
function.
To create the image:-
array = numpy.array(pixels, dtype = numpy.uint8)
new_image = Image.fromarray(array)
new_image.save('user_key.png')
Note that pixels is a list of list of tuples of integers [[(...), (...)], [(...), (...)], ...]
and this is the object that carries keys
To get keys from the image:-
im = Image.open(image_path)
return list(im.getdata())
Now I'm not able to directly store the AES key
, assuming I generate it with Random.get_random_bytes(AES.key_size)
from the Crypto module.
How can I generate a cryptographically secure key but also retrieve it by using one of the keys in pixels
, as in a tuple of integers?
Edit:-
To elaborate, the pixels object is a list of list of tuples of integers, each tuple contains 3 integers and each integer can range from 0 to 255. The 0th index of the pixels object may look like this-
[(69, 147, 245), (120, 212, 198), ...]
The key_list
object I'm referring to is actually list(im.getdata())
. This is a list of tuples of integers, each tuple contains 3 integers and each integer can range from 0 to 255. This looks like this-
[(69, 147, 245), (120, 212, 198)....]
Hence, the 0th index of key_list will be (69, 147, 245)
I need to store the AES key on par with these values. I'd ideally like to store the AES key as a tuple of 3 integers ranging from 0 to 255. So yes, I need to convert the AES key into a tuple and then store it in pixels
.
One more key detail, the tuples contain 3 integers because they represent the RGB values respectively to create the image. I believe the tuple can be made with 4 integers as well to represent RGBA values. So that will solve the multiple of 3 problem.
But there's another problem. Each tuple in pixels
is actually generated through [i for i in itertools.product(range(256), repeat=3)]
. In order to generate a tuple of 4 integers instead of 3 I'll have to change repeat=3
to repeat=4
, this will raise MemoryError.