I'm working on a school project where we are given a cipher file encrypted using openssl with AES-128 cipher and CBC mode. It is indicated that the file is encrypted with no salt and IV and has less than 16 characters in password which is appended by a number from 0-9 at the end. We are given a dictionary (words.txt) and the cipher file and I have to write a script to find the key and decrypt the content of the file.
For testing purposes, I encrypted a plaintext file using the following openssl command:
openssl enc -e -aes-128-cbc -in plain.txt -out test.txt -nosalt -pass pass:Zoroastrian6
And I am decrypting it using the following command:
openssl enc -d -aes-128-cbc -in test.txt -out test2.txt -nosalt -pass pass:Zoroastrian6
Which works fine. The password I have used for it is the last entry from the words.txt provided by my lecturer. This is what I have written so far:
#!/bin/bash
filename="words.txt"
let flag=0
while read line
do
# Second loop to append 0-9 at the end of each line read from
# the text file
let count=0
while [ $count -lt 10 ];
do
comm=$(openssl enc -d -aes-128-cbc -in test.txt -out test2.txt -nosalt -pass pass:$line$count 2>/dev/null)
# If the program exits without an error, we print
# the password and exit the loop before setting the
# exit flag to be checked in the first loop to break
# out of it.
if [ $? -eq 0 ]; then
echo "$line$count"
flag=1
break
fi
((count++))
done
# If flag is set here, it means the password was found
# and we can exit the loop.
if [ $flag -eq 1 ]; then
break
fi
done < "$filename"
The problem is that instead of reaching till the end of the file where the word "Zoroastrian" is located, it exits with the password "abstinent8". I don't know how to debug this as I'm new to bash scripting. Is there a way to check if openssl exited without any errors or a way to check if the decryption gave an error?
EDIT: words.txt can be found here.