# 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
cat header.txt body.ecb.bin > Tux.ecb.ppm


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.

The video explanation: https://www.youtube.com/watch?v=wbHkVZfCNuE

Some slides: https://www.blackhat.com/docs/eu-14/materials/eu-14-Apvrille-Hide-Android-Applications-In-Images.pdf (not the same as the op asked but the idea is the very same)

• Great example that even encrypted the information is still "easily" extracted. – aggsol Dec 16 '16 at 14:53
• As others have noted, "yes". If you're trying to show the effects of how the encryption is applied, the wikipedia page for Electronic Code Book vs Cipher Block Chaining is worth a quick look - scroll down to the three images of Tux (a penguin). – Rob Swarbrick Nov 6 '19 at 15:46

Yes, you can if you can carefully choose the key and IV to create a cipher text that forms an image format. This is normally unlikely to happen with a random key and IV.

• Is there any method to find such a key? – Rakesh Nair Jan 18 '17 at 9:57
• Sorry, I am unaware of any such 'method'. However, I presume that such a method will eventually have to find the key from its cipher which is nothing different than a known plain text attack. – vishnuvp Aug 19 '17 at 11:52
• Err, wouldn't this be tantamount to having 'broken' any current encryption algorithm? If you can encipher a banana to look like a giraffe, that would afford you total control over encryption. The other answer is a bit of a trick and not so specific. – Paul Uszak Nov 7 '19 at 0:10