# If you encrypt an image (AES), is it still an image and can you view it?

Can you encrypt an image (with AES) such that you can open it as a png/jpeg?

• No. What are you trying to achieve? Prevent modifying image? – LightBit Dec 3 '15 at 17:40
• No I am trying to show a visual representation of the image after encrypting.. being able to open as a image (image viewer) – JohnAndrews Dec 3 '15 at 17:42
• You can do that. You have to encrypt only contents (pixels) of image. – LightBit Dec 3 '15 at 17:45
• @lightbit. How do you do that? Do uou have an example? – JohnAndrews Dec 3 '15 at 17:46
• Depends on image format. You can do this by opening image with library which will give you array of pixels or just skip header while encrypting (header might contain checksum which you must replace). – LightBit Dec 3 '15 at 17:59

If you aim to show the effect of the encryption, meaning a scrabled image you have to encrypt only the "image data" and keep the file structure unchaned. A simple tutorial can be found here: https://blog.filippo.io/the-ecb-penguin/. Author uses ppm files, but you can easily adapt to your file format.

# First convert the Tux to PPM with Gimp
# Then take the header apart
tail -n +5 Tux.ppm > body.bin
# Then encrypt with ECB (experiment with some different keys)
openssl enc -aes-128-ecb -nosalt -pass pass:"ANNA" -in body.bin -out body.ecb.bin
# And finally put the result together and convert to some better format with Gimp


Here’s an example (using the key “ECB”):

You actually can. You actually can do even more: encrypt a file format into an other. You need to play with key and IV to get that the message input is "well" transformed by the encryption. If you control the key and the IV and the initial and final file you ave enough freedom to achieve your aim. Ange Albertini made a proof of concept. Ablog post explaining the idea is available here: http://blog.fortinet.com/post/angecryption-at-insomni-hack

Roughly you want that the encryption of the file header (with the magic number, identifying the file type) is encrypted into a valid file header. Then you can play with data chunks and segment to get one image or the other.