v7.1.2-34 Latest Stable Release

Encipher or Decipher an Image

Learn how ImageMagick applies cipher functions to encrypt and decrypt image pixels. Explore supported algorithms, usage examples, and how ciphers enhance image security and controlled access.

quickstart.sh
# Install via your favorite Package Manager
$ brew install imagemagick
# Convert PNG to WebP with compression
$ magick input.png -quality 85 output.webp
# Resize image maintaining aspect ratio
$ magick input.jpg -resize 800x600^ output.jpg

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 a select group of 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, though the content appears as gibberish rather than the original image.

Encipher an Image

Use the -encipher option to scramble your image so that it is unrecognizable. This option requires a filename containing 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 is an example where 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. This option also requires a filename containing 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

Format & Pixel Support

Some formats (such as JPEG or GIF) do not support enciphered pixels. To ensure your format is supported, encipher a test image and verify you can restore its original content before processing additional images. Furthermore, because formats may only support 8-bit and RGB (TrueColor), you may want to include options like -depth 8 -type TrueColor before your output filename.

Passphrase Security

Passphrases can combine letters and symbols and should be a minimum of 12 characters to help ensure privacy. Ensure your passphrase file permissions prevent unauthorized reading. Additionally, if you lose or forget your passphrase, your original image content is lost forever. Note that ImageMagick only scrambles image pixels; metadata remains untouched and readable.

Cryptographic Details & Limitations

ImageMagick uses AES in Counter (CTR) mode to scramble an image's pixel data. The current implementation derives both the AES key and the initial counter (IV) deterministically from the user-supplied passphrase: the first half becomes the AES key, while the second half is hashed alongside image dimensions to produce the IV. Because this IV relies solely on the passphrase and dimensions, encrypting multiple images with the same passphrase and dimensions produces identical IVs and keystreams.

CTR mode requires a unique, non-repeating IV for every encryption under the same key. Reusing an IV causes keystream reuse, allowing attackers to recover information about plaintexts by XORing ciphertexts. Because of this deterministic IV construction, ImageMagick's encipher feature is suitable only for casual obfuscation rather than security-sensitive encryption. Avoiding keystream reuse would require supplying a unique passphrase per image, which is impractical and falls short of modern cryptographic expectations.

Interoperability and Examples

Currently, only ImageMagick can restore enciphered image content, though standard ciphers and modes are used so other vendors could theoretically support it. Small practical examples of image enciphering can be found in the IM Examples under Encrypting Image Data.