Skip to content

Instantly share code, notes, and snippets.

@jtdowney
Last active September 1, 2020 20:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtdowney/6293784 to your computer and use it in GitHub Desktop.
Save jtdowney/6293784 to your computer and use it in GitHub Desktop.
Create ECB pattern images

This gist describes how to use the ImageMagick and OpenSSL command line tools to encrypt an image using ECB mode.

First we need to gather some information about the original image. This will tell us what size to use in our final step.

$ identify Braintree.png
Braintree.png PNG 898x229 898x229+0+0 8-bit sRGB 65KB 0.000u 0:00.000

Looks like the image is 898x229.

Next we need to convert the image into the RGBA format. This is a simple binary format that only contains uncompressed pixel data and no meta information, like image dimensions.

$ convert -depth 32 Braintree.png Braintree.rgba

Next we will use the OpenSSL command line utility to encrypt the RGBA file.

$ openssl enc -aes-128-ecb -e -in Braintree.rgba -out Braintree-ecb.rgba -k toomanysecrets

This will encrypt the file for us using a key derived from the given passphrase. After we have our encrypted file it is time to turn it back into an image.

$ convert -size 898x229 -depth 32 Braintree-ecb.rgba Braintree-ecb.png

We use the size we gathered in the first step to reconstruct an image of the proper dimensions. Now we have an image that has been encrypted under ECB mode. The steps are easy to repeat with CBC or any other supported cipher mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment