$site=strtolower("ImageMagick");

Encipher an Image • Decipher an Image • Encipher and Decipher Caveats

Most images, by design, are made to be viewed often and by many people. Web images, for example, may be viewed hundreds of times a day by a multitude of visitors. However, in some cases, you may want to keep a particular image private so that only you or perhaps a select group of your friends or web visitors can view it. ImageMagick permits you to scramble your images such that unless someone knows your passphrase, they will be unable to view the original content.

You could use an enciphering utility to scramble your image but they typically scramble the entire file making it unrecognizable as an image format. With ImageMagick, only the pixels are scrambled. The scrambled image continues to be recognized as an image and will even display in your web page. However, the content appears as gibberish, nothing like the original content.

## Encipher an Image

Use the -encipher option to scramble your image so that it is unrecognizable. The option requires a filename that contains your passphrase. In this example we scramble an image and save it in the PNG format:

    $ magick rose.jpg -encipher passphrase.txt rose.png

Here we encipher an image using another image as the passphrase:

    $ magick rose.jpg -encipher smiley.gif rose.png

## Decipher an Image

Use the -decipher option to unscramble your image so that it is recognizable once again. The option requires a filename that contains your passphrase. In this example we unscramble an image and save it in the JPEG format:

    $ magick rose.png -decipher passphrase.txt rose.jpg

## Encipher and Decipher Caveats

Some formats do not support enciphered pixels-- the JPEG or GIF format, for example. To ensure your image format is supported, encipher a test image and verify you can restore its original content before you encipher any additional images in that format.

The image format may only support 8-bit and RGB (TrueColor). As such you may like to include the options "-depth 8 -type TrueColor" before the output filename.

The passphrase can be any combinations of letters and symbols. It should be a minimum of 12 character combinations to help ensure your image remains private. Also make sure your passphrase file permissions prevent others from reading it otherwise unintended users may be able to view the original image content.

You can only restore the original image content if you know your passphrase. If you lose or forget it, your original image content is lost forever.

ImageMagick only scrambles the image pixels. The image metadata remains untouched and readable by anyone with access to the image file.

ImageMagick uses AES in Counter (CTR) mode to scramble the pixel data of an image. The current implementation derives both the AES key and the IV (initial counter) deterministically from the user‑supplied passphrase: the first half of the passphrase becomes the AES key, and the second half is hashed together with the image dimensions to produce the IV. Because this IV is derived solely from the passphrase and the image’s width and height, encrypting multiple images with the same passphrase and the same dimensions produces the same IV and therefore the same keystream.

CTR mode requires a unique, non‑repeating IV for every encryption under the same key. Reusing an IV in CTR mode causes keystream reuse, which allows attackers to recover information about the plaintexts by XORing ciphertexts. For this reason, the current deterministic IV construction does not provide cryptographically strong confidentiality.

To avoid keystream reuse and the resulting information leaks, users would need to supply a different passphrase for every image, which is impractical and still does not meet modern cryptographic expectations. ImageMagick’s encipher feature is therefore suitable only for casual obfuscation, not for security‑sensitive encryption.

Currently only ImageMagick can restore your enciphered image content. We use a standard cipher and mode so other vendors could support enciphered image content.

Some small practical examples of image enciphering can be found in IM Examples Encrypting Image Data.

About ImageMagick