#encrypting an image using AES
from Crypto.Cipher import AES
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
filename = 'path to input_image.jpg'
with open(filename, 'rb') as f:
content = f.read()
#converting the jpg to hex, trimming whitespaces and padding.
content = binascii.hexlify(content)
binascii.a2b_hex(content.replace(' ', ''))
content = pad(content)
#16 byte key and IV
#thank you stackoverflow.com
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
ciphertext = obj.encrypt(content)
#is it right to try and convert the garbled text to hex?
ciphertext = binascii.hexlify(ciphertext)
obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
plaintext = obj2.decrypt(ciphertext)
#content = content.encode('utf-8')
#the plaintext here matches the original hex input file as it should
with open('path to - AESimageDecrypted.txt', 'wb') as g:
The question here is how to display encrypted image as an image without decrypting it.
The encrypted contents are not an image and cannot be unambiguously represented as an image. The best that can be done is to treat it as a bitmap, i.e. each binary value represents the intensity of some color at some coordinate.
It seems logical to treat the data as 3 bytes per pixel: RGB RGB RGB...
Images are 2D and encrypted data is just a list of bytes. Again, several options are valid. Let's say it is a square image (NxN pixels).
To create the image, I would use PIL / Pillow:
from PIL import Image # calculate sizes num_bytes = len(cyphertext) num_pixels = int((num_bytes+2)/3) # 3 bytes per pixel W = H = int(math.ceil(num_pixels ** 0.5)) # W=H, such that everything fits in # fill the image with zeros, because probably len(imagedata) < needed W*H*3 imagedata = cyphertext + '\0' * (W*H*3 - len(cyphertext)) image = Image.fromstring('RGB', (W, H), imagedata) # create image image.save('C:\\Temp\\image.bmp') # save to a file
BTW, this can be done with absolutely any string of bytes, not just encrypted images.