Shift Cipher Assignment Answers

    Encryption  

    Encode the following message using a shift 4 cipher, as Francis Bacon supposedly did. Note: You may use the encryption tool described here or you may do this by hand.

      Francis Bacon
    shift_encrypt('francis bacon',4)
    'jvergmw fegsr'
    

    Now encode it using a shift 15 cipher.

    shift_encrypt('Francis Bacon',15)
    'Fgpcrxh Bprdc'
     >shift_encrypt('francis bacon',15)
    'ugpcrxh qprdc'
    
    Decryption  

    The following messages have been encoding using a shift cipher with key 8. Decode.

    1. emtkwum bw bpm eiksg ewztl wn kzgxbwozixpg >shift_encrypt('emtkwum bw bpm eiksg ewztl wn kzgxbwozixpg',18) 'welcome to the wacky world of cryptography'
    2. tivociom qa zmlcvlivb shift_encrypt('tivociom qa zmlcvlivb',18) 'language is redundant'
    Cryptography  

    The following message has been encoded using a shift cipher. What is the key?

      n puvyq funyy yrnq gurz

    Answer 13

    The brute force method:

    Cipher text:  n puvyq funyy yrnq gurz
    ---------------------------
        Shift: 0: n puvyq funyy yrnq gurz
        Shift: 1: o qvwzr gvozz zsor hvsa
        Shift: 2: p rwxas hwpaa atps iwtb
        Shift: 3: q sxybt ixqbb buqt jxuc
        Shift: 4: r tyzcu jyrcc cvru kyvd
        Shift: 5: s uzadv kzsdd dwsv lzwe
        Shift: 6: t vabew latee extw maxf
        Shift: 7: u wbcfx mbuff fyux nbyg
        Shift: 8: v xcdgy ncvgg gzvy oczh
        Shift: 9: w ydehz odwhh hawz pdai
        Shift: 10: x zefia pexii ibxa qebj
        Shift: 11: y afgjb qfyjj jcyb rfck
        Shift: 12: z bghkc rgzkk kdzc sgdl
        Shift: 13: a child shall lead them
        Shift: 14: b dijme tibmm mfbe uifn
        Shift: 15: c ejknf ujcnn ngcf vjgo
        Shift: 16: d fklog vkdoo ohdg wkhp
        Shift: 17: e glmph wlepp pieh xliq
        Shift: 18: f hmnqi xmfqq qjfi ymjr
        Shift: 19: g inorj yngrr rkgj znks
        Shift: 20: h jopsk zohss slhk aolt
        Shift: 21: i kpqtl apitt tmil bpmu
        Shift: 22: j lqrum bqjuu unjm cqnv
        Shift: 23: k mrsvn crkvv vokn drow
        Shift: 24: l nstwo dslww wplo espx
        Shift: 25: m otuxp etmxx xqmp ftqy
    
    Notice just a shifting the one-letter word at the beginning helps, sinc ethere arent that many 1-letter words in English:
    Cipher text:  n puvyq funyy yrnq gurz
    ---------------------------
        Shift: 0: n
        Shift: 1: o
        Shift: 2: p
        Shift: 3: q
        Shift: 4: r
        Shift: 5: s
        Shift: 6: t
        Shift: 7: u
        Shift: 8: v
        Shift: 9: w
        Shift: 10: x
        Shift: 11: y
        Shift: 12: z
        Shift: 13: a
        Shift: 14: b
        Shift: 15: c
        Shift: 16: d
        Shift: 17: e
        Shift: 18: f
        Shift: 19: g
        Shift: 20: h
        Shift: 21: i
        Shift: 22: j
        Shift: 23: k
        Shift: 24: l
        Shift: 25: m
    
    Only 13 and 21 look like 1-letter words. So we carry on with those:
    Cipher text:  n puvyq funyy yrnq gurz
    ---------------------------
        Shift: 13: a child
        Shift: 21: i kpqtl
    
    And now 13 looks like the winner.
    Affine
    encryption
     

     >affine_encrypt('attack at dawn',3,2)
    'chhcig ch lcqp'
    
    Affine decryption  

    Decrypt the following message assuming it is an affine cipher with a slope of 3 and a shift of 2:

      goov wskb ixap kv

    If (m,b) is the encryption key, then (m-1, - (m-1 * b)) is the decryption key.

    We need to find the inverse of 3. The multiplication table for 3 is:

      Multiplication table for 3 modulus 26
        0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      3 0 3 6 9 12 15 18 21 24 1 4 7 10 13 16 19 22 25 2 5 8 11 14 17 20 23
    We see by inspecting the table that the inverse of 3 mod 26 is 9. (Later we'll learn a better way for fidning inverses).

    So therefore the decryption key should be:

    (9, -(9*2)) = (9,-18) = (9,8)
    
    We encrypt using this decryption key to decode:
     >affine_encrypt('goov wskb ixap kv',9,8)
    'keep your chin up'
    
    Thus for example:
    'g' => 'k'  (9,8)
    encoding 'g' = 6
    6 * 9 = 2 mod 26
    2 + 8 = 10
    decoding 10 = 'k'
    

    Verifying:

     >affine_encrypt('keep your chin up',3,2)
    'goov wskb ixap kv'
    
    Finding
    Affine
    Keys
     

    Determine the shift and slope for the following affine encryption scheme:

      Plain Text a b c d e f g h i j k l m n o p q r s t u v w x y z
      Cipher Text f m t a h o v c j q x e l s z g n u b i p w d k r y

    Compute encodings:

      Plain Text a b c d e f g h i j k l m n o p q r s t u v w x y z
      Encoding 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      Cipher Codes 5 12 19 0 7 14 21 2 9 16 23 4 11 18 25 6 13 20 1 8 15 22 3 10 17 24
      Cipher f m t a h o v c j q x e l s z g n u b i p w d k r y

    the simplest way to do this:

    1. The encoding of 'a'(=0) tells us the shift (b):
      (m * 0) + b = 5
      b = 5
      
    2. Now we plug this info into the encoding for 'b'(=1):
      (m * 1) + 5 = 12
      m + 5 =12
      m = 7
      
    3. So the key is (7,5).
    4. Verifying with another example 'd'(=3):
      (7 * 3) + 5 = 26 = 0 
      decoding 0 = 'a'
      
      Yes.
    Valid
    Affine Keys
     

    Suppose we change the ALPHABET we're encoding to include " "(space). That's one more character. That means we're now doing arithmetic mod 27 instead of mod 26.

    Previously (in mod 26) our valid slopes were all the odd numbers minus 13. What is our new set of valid slopes (in mod 27)?

    Valid slopes must be relatively prime to 27. The only factors of 27 are powers of 3. Therefore any multiple of 3 is excluded, leaving:

    1 2 4 5 6 7 8 10 11 13 14 16 17 19 20 22 23 25 26