BASH   39
avalanche test
Guest on 8th February 2023 02:56:49 AM


  1. #!/bin/bash
  2. # Count the number of bits that in ciphertext when changing one bit
  3. # of input key
  4.  
  5. # STEP 1. INITIALISE VARIABLES
  6. # ============================
  7.  
  8. # The plaintext to encrypt
  9. plaintext="stevengo"
  10.  
  11. # Keys. The original key, and then set of modified keys, differing
  12. # from the original key by 1 bit.
  13. key="0000000000000001"
  14. modkeys="0000000000000002 0000000000000004 0000000000000008 0000000000000010 0000000000000020"
  15.  
  16. # IV
  17. iv="0000000000000000"
  18.  
  19. # Base of the filename. filebase.txt is plaintext, filebase.enc is
  20. # ciphertext.
  21. filebase="name"
  22.  
  23. # STEP 2. ENCRYPT THE PLAINTEXT
  24. # =============================
  25.  
  26. # Create the plaintext file. The -n option for echo means a newline
  27. # will NOT be added to the end, so the file is exactly 8 bytes (since
  28. # the input string is 8 characters). That is, 64 bits or 1 block.
  29. echo -n ${plaintext} > ${filebase}.txt
  30.  
  31. # Encrypt the plaintext to get the original ciphertext
  32. openssl enc -des-ecb -e -in ${filebase}.txt -out ${filebase}.enc -iv ${iv} -K ${key} -nopad
  33.  
  34.  
  35. # STEP 3. CONVERT CIPHERTEXT TO BINARY STRING
  36. # ===========================================
  37.  
  38. # Later we will compare the binary ciphertext with the original key
  39. # against the binary ciphertexts for the modified keys. To do so,
  40. # we will save the binary value as a set of 0's and 1's in a text file
  41. # then compare the text files.
  42.  
  43. # xxd is used to get the binary form of the ciphertext, then cut
  44. # is used to select the binary value from the output of xxd. The
  45. # result, a string of 64 0's or 1's, is saved in a .bin file.
  46. xxd -b -c 8 -g 8 ${filebase}.enc | head -1 | cut -d " " -f 2 > ${filebase}.bin
  47.  
  48. # Display the binary ciphertext
  49. cat ${filebase}.bin
  50.  
  51. echo " "
  52.  
  53. # STEP 4. ENCRYPT USING MODIFIED KEYS
  54. # ===================================
  55.  
  56. # For each key in the list of modified keys
  57. for k in $modkeys
  58. do
  59.         # Encrypt using the modified key
  60.         openssl enc -des-ecb -e -in ${filebase}.txt -out ${filebase}-${k}.enc -iv ${iv} -K ${k} -nopad
  61.  
  62.         # Convert the ciphertext to a binary string in a file
  63.         xxd -b -c 8 -g 8 ${filebase}-${k}.enc | head -1 | cut -d " " -f 2 > ${filebase}-${k}.bin
  64.  
  65.         # Display the binary ciphertext
  66.         cat ${filebase}-${k}.bin
  67.  
  68.         # About the count the bits different ...
  69.         echo -n "Bits different from original ciphertext: "
  70.  
  71.         # Compare the binary string with the original, counting the
  72.         # number of values that differ
  73.         cmp -l ${filebase}.bin ${filebase}-${k}.bin | wc -l
  74.  
  75.         echo " "
  76. done

Raw Paste

Login or Register to edit or fork this paste. It's free.