6
$\begingroup$

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

$\endgroup$
5
  • 1
    $\begingroup$ No. What are you trying to achieve? Prevent modifying image? $\endgroup$
    – LightBit
    Commented Dec 3, 2015 at 17:40
  • $\begingroup$ No I am trying to show a visual representation of the image after encrypting.. being able to open as a image (image viewer) $\endgroup$
    – WJA
    Commented Dec 3, 2015 at 17:42
  • 1
    $\begingroup$ You can do that. You have to encrypt only contents (pixels) of image. $\endgroup$
    – LightBit
    Commented Dec 3, 2015 at 17:45
  • $\begingroup$ @lightbit. How do you do that? Do uou have an example? $\endgroup$
    – WJA
    Commented Dec 3, 2015 at 17:46
  • 1
    $\begingroup$ 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). $\endgroup$
    – LightBit
    Commented Dec 3, 2015 at 17:59

4 Answers 4

19
$\begingroup$

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”):

Crypto.SE logo as “ECB penguin” alternative


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

An example: https://code.google.com/p/corkami/source/detail?r=1906

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)

$\endgroup$
2
  • $\begingroup$ Great example that even encrypted the information is still "easily" extracted. $\endgroup$
    – aggsol
    Commented Dec 16, 2016 at 14:53
  • $\begingroup$ 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). $\endgroup$ Commented Nov 6, 2019 at 15:46
1
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Is there any method to find such a key? $\endgroup$ Commented Jan 18, 2017 at 9:57
  • $\begingroup$ 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. $\endgroup$
    – vishnuvp
    Commented Aug 19, 2017 at 11:52
  • 1
    $\begingroup$ 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. $\endgroup$
    – Paul Uszak
    Commented Nov 7, 2019 at 0:10
0
$\begingroup$

Sure you can. Use:

gpg -c normalimage.png

This will normally bring up a pinentry dialog for inputing the password/key.

Then to view it, you could use:

gpg -d -o - /mnt/Documents/normalimage.png.gpg | convert - -resize 500 x:

If desired, be sure to delete the unencrypted image.

$\endgroup$
1
  • $\begingroup$ Welcome. This answer is on the aspect of using a particular cryptography software. The question, comments, and other answers however are on the aspect of preserving parsable file format under encryption. So while we appreciate your effort on this answer, we hope you can read more into the context next time. $\endgroup$
    – DannyNiu
    Commented Dec 12, 2023 at 12:12
0
$\begingroup$

You could take an image, decode it to an array of n*m RGB or RGBA pixels, encrypt it to the same number of bytes, and turn the bytes back into an image.

On the other hand, if you have a very simple image format, starting with the width and height in two bytes each, then encryption will turn these two values into unpredictable numbers. An image reader would think that your 1024x768 pixel image is for example 14,527x46,124 pixels, and things will go wrong. It might be enough to not encrypt the first four bytes.

I think the BMP format is simple enough that not encrypting a header is enough. Some information would leak, but if you have the size of the image in bytes then you can make an educated guess about the image header anyway, and encrypting it opens a chance for a known clear text attack.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.