Skip to content

Instantly share code, notes, and snippets.

@patrickfav
Created November 17, 2018 14:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save patrickfav/13b2f727eaf91e3a72d87ac427485cb1 to your computer and use it in GitHub Desktop.
Save patrickfav/13b2f727eaf91e3a72d87ac427485cb1 to your computer and use it in GitHub Desktop.
Encrypt .ppm file with AES-ECB to show ECB will reveal patterns
#!/bin/sh
# This is part of my blog about AES: https://medium.com/p/7616beaaade9
# Inspired by https://blog.filippo.io/the-ecb-penguin/
# Convert your image to .ppm with Gimp or Photoshop
#
# Usage: ./ecb_img <image file as ppm> <password>
# extract header and body
head -n 4 $1 > $1.header.txt
tail -n +5 $1 > $1.body.bin
# encrypt with ecb and given password
openssl enc -aes-128-ecb -nosalt -pass pass:"$2" -in $1.body.bin -out $1.body.ecb.bin
# reassemble image
cat $1.header.txt $1.body.ecb.bin > $1.ecb.ppm
# Clean up temp files
rm $1.header.txt
rm $1.body.bin
rm $1.body.ecb.bin
echo "encrypted image to $1.ecb.ppm"
@q000325
Copy link

q000325 commented Jan 12, 2019

head -n 4 $1 > $1.header.txt
tail -n +5 $1 > $1.body.bin

Maybe it should be something like this:

head -n 3 $1 > $1.header.txt
tail -n +4 $1 > $1.body.bin

@Youssef-Beltagy
Copy link

The number of header lines depends on the whitespaces used: https://netpbm.sourceforge.net/doc/ppm.html

If spaces are used, then there is only one header line. If next line characters are used, then you would need four. Anything in between is fair game.

A more robust implementation would account for different whitespaces.

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