Code

Prove the Round Trip

A shift cipher was cracked in seconds. So spies used a cleverer trick: a keyword cipher (also called the Vigenère cipher). Each letter gets its own shift, taken from a keyword.

With keyword lemon, the shifts are l=11, e=4, m=12, o=14, n=13, then repeat. Only letters use up the keyword; spaces are skipped. attack at dawn becomes lxfopv ef rnhr.

To pick the keyword letter, keep a counter i and use keyword[i % len(keyword)]. The % makes the keyword repeat.

It was called "the unbreakable cipher" for 300 years. Today it can be cracked too, so it's still just for fun.

Prove it works

A cipher is useless if decoding doesn't give back the original. Programmers test this on many messages: does decode(encode(m)) equal m every time? This is a round-trip test.

Assignment

shift_char and is_letter are ready for you.

  1. keyword_cipher(message, keyword, direction) shifts each letter by its keyword letter's position, times direction. direction is 1 to encode and -1 to decode. Non-letters stay the same and don't use up a keyword letter.
  2. round_trip_ok(messages, keyword) returns True if encoding then decoding gives back every message, otherwise False.