Can you encrypt an image (with AES) such that you can open it as a png/jpeg?
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 head -n 4 Tux.ppm > header.txt 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)