#!/bin/bash # Count the number of bits that in ciphertext when changing one bit # of input key # STEP 1. INITIALISE VARIABLES # ============================ # The plaintext to encrypt plaintext="stevengo" # Keys. The original key, and then set of modified keys, differing # from the original key by 1 bit. key="0000000000000001" modkeys="0000000000000002 0000000000000004 0000000000000008 0000000000000010 0000000000000020" # IV iv="0000000000000000" # Base of the filename. filebase.txt is plaintext, filebase.enc is # ciphertext. filebase="name" # STEP 2. ENCRYPT THE PLAINTEXT # ============================= # Create the plaintext file. The -n option for echo means a newline # will NOT be added to the end, so the file is exactly 8 bytes (since # the input string is 8 characters). That is, 64 bits or 1 block. echo -n ${plaintext} > ${filebase}.txt # Encrypt the plaintext to get the original ciphertext openssl enc -des-ecb -e -in ${filebase}.txt -out ${filebase}.enc -iv ${iv} -K ${key} -nopad # STEP 3. CONVERT CIPHERTEXT TO BINARY STRING # =========================================== # Later we will compare the binary ciphertext with the original key # against the binary ciphertexts for the modified keys. To do so, # we will save the binary value as a set of 0's and 1's in a text file # then compare the text files. # xxd is used to get the binary form of the ciphertext, then cut # is used to select the binary value from the output of xxd. The # result, a string of 64 0's or 1's, is saved in a .bin file. xxd -b -c 8 -g 8 ${filebase}.enc | head -1 | cut -d " " -f 2 > ${filebase}.bin # Display the binary ciphertext cat ${filebase}.bin echo " " # STEP 4. ENCRYPT USING MODIFIED KEYS # =================================== # For each key in the list of modified keys for k in $modkeys do # Encrypt using the modified key openssl enc -des-ecb -e -in ${filebase}.txt -out ${filebase}-${k}.enc -iv ${iv} -K ${k} -nopad # Convert the ciphertext to a binary string in a file xxd -b -c 8 -g 8 ${filebase}-${k}.enc | head -1 | cut -d " " -f 2 > ${filebase}-${k}.bin # Display the binary ciphertext cat ${filebase}-${k}.bin # About the count the bits different ... echo -n "Bits different from original ciphertext: " # Compare the binary string with the original, counting the # number of values that differ cmp -l ${filebase}.bin ${filebase}-${k}.bin | wc -l echo " " done