Skip to content

Instantly share code, notes, and snippets.

@curtmack
Created January 27, 2021 17:48
Show Gist options
  • Save curtmack/71a633b7144bf55d5034a6d9261dbc1c to your computer and use it in GitHub Desktop.
Save curtmack/71a633b7144bf55d5034a6d9261dbc1c to your computer and use it in GitHub Desktop.
Shell script to play with ECB encryption.
#!/bin/sh
# Shell script to play with ECB encryption.
# Requires ImageMagick and OpenSSL commandline tools.
# See https://twitter.com/curtmackevo/status/1354471167793520641
while getopts f:o: c
do
case $c
in
(f) IMAGE="$OPTARG";;
(o) OUTPUT="$OPTARG";;
(*) echo Unrecognized option $c; exit 1;;
esac
done
if [ -z "$IMAGE" -o -z "$OUTPUT" ]
then
echo "usage: $0 -f <image> -o <output>"
exit 1
fi
# Get image properties
if identify "$IMAGE" > /dev/null 2>&1
then
SIZE=`identify -format '%G' "$IMAGE"`
DEPTH=`identify -format '%z' "$IMAGE"`
else
echo "Not an image: $IMAGE"
exit 1
fi
# We need two temporary files for conversions; we can swap back and
# forth between them
TMP1=`tempfile`
TMP2=`tempfile`
# Create random key
KEY=`openssl rand -hex 32`
# Convert the image to RGB
convert "$IMAGE" "rgb:$TMP1"
# Encrypt the image
openssl aes-256-ecb -e -K "$KEY" -in "$TMP1" -out "$TMP2"
# Convert back into a PNG at the output location
convert -size "$SIZE" -depth "$DEPTH" "rgb:$TMP2" "$OUTPUT"
# Delete temp files
rm "$TMP1" "$TMP2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment