Skip to content

Instantly share code, notes, and snippets.

@homelinen
Created October 10, 2013 21:06
Show Gist options
  • Save homelinen/6925670 to your computer and use it in GitHub Desktop.
Save homelinen/6925670 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Implementation of a Known Plaintext Attack
#
# Author: Calum Gilchrist
# email: [email protected]
tmp_file=temp-$RANDOM.txt
if [[ ! $# -eq 2 ]]; then
echo "Usage: known_attack.sh encrypted_file plaintext_file"
exit 1
fi
in_file=$1
orig_file=$2
sha1=$(shasum $orig_file | grep -Eo "^([0-9]|[a-Z])*")
while read line; do
# Decrypt the file without a salt and use a word from words.txt as pass
openssl enc -aes-128-cbc -d \
-nosalt \
-in $in_file \
-out $tmp_file \
-pass pass:$line \
2>/dev/null
if [[ $? -eq 0 ]]; then
sha2=$(shasum $tmp_file | grep -Eo "^([0-9]|[a-Z])*")
# Compare shas, if password found, stop looking
if [[ $sha1 == $sha2 ]]; then
echo Password: $line
break
fi
fi
done < words.txt
rm -v $tmp_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment