#!/bin/bash # Show the impact of errors in ciphertext on the different modes of operation # List of ciphers/modes to use (using format of OpenSSL) modes="des-ecb des-cbc des-cfb des-ofb" # Base of plaintext file plaintext="repeated" # Key key="0000000000000013" # IV iv="0000000000000000" # For each cipher/mode ... for m in ${modes} do echo "Mode: $m" # Encrypt the plaintext openssl enc -${m} -e -in ${plaintext}.txt -out ${plaintext}-$m.enc -iv ${iv} -K ${key} echo "Original ciphertext:" xxd ${plaintext}-$m.enc # Find the first word of ciphertext firstword=`xxd ${plaintext}-$m.enc | head -1 | cut -d " " -f 2 | cut -c 1` # Find the second word of ciphertext secondword=`xxd ${plaintext}-$m.enc | head -1 | cut -d " " -f 2 | cut -c 2` # We are going to change the second word, incrementing by 1 if [ "${secondword}" = "0" ] then newbyte="1"; elif [ "${secondword}" = "1" ] then newbyte="2"; elif [ "${secondword}" = "2" ] then newbyte="3"; elif [ "${secondword}" = "3" ] then newbyte="4"; elif [ "${secondword}" = "4" ] then newbyte="5"; elif [ "${secondword}" = "5" ] then newbyte="6"; elif [ "${secondword}" = "6" ] then newbyte="7"; elif [ "${secondword}" = "7" ] then newbyte="8"; elif [ "${secondword}" = "8" ] then newbyte="9"; elif [ "${secondword}" = "9" ] then newbyte="a"; elif [ "${secondword}" = "a" ] then newbyte="b"; elif [ "${secondword}" = "b" ] then newbyte="c"; elif [ "${secondword}" = "c" ] then newbyte="d"; elif [ "${secondword}" = "d" ] then newbyte="e"; elif [ "${secondword}" = "e" ] then newbyte="f"; else newbyte="0"; fi # Replace the first byte with the modified byte sed 's/\x'${firstword}${secondword}'/\x'${firstword}${newbyte}'/' ${plaintext}-$m.enc > ${plaintext}-$m-mod.enc echo "Modified ciphertext" xxd ${plaintext}-$m-mod.enc # Decrypt the original ciphertext openssl enc -${m} -d -in ${plaintext}-$m.enc -out decrypt-$m.txt -iv ${iv} -K ${key} # Decrypt the modified ciphertext openssl enc -${m} -d -in ${plaintext}-$m-mod.enc -out decrypt-$m-mod.txt -iv ${iv} -K ${key} echo "Original decrypted plaintext" xxd decrypt-$m.txt echo "Modified decrypted plaintext" xxd decrypt-$m-mod.txt echo " " done