23 "def sanitised(text):\n",
24 " return ''.join(letter.lower() for letter in text if letter in string.ascii_letters)"
37 "'thequickbrownfoxjumpedoverthelzydoga'"
42 "output_type": "execute_result"
46 "sanitised('The quick brown fox! jumped OVER the l\\a!z%y* dog....a')"
51 "execution_count": 12,
57 "def encipher_letter(key, current):\n",
58 " new = (ord(key) + ord(current) - 2 * ord('a')) % 26\n",
59 " return chr(new + ord('a'))"
70 "def decipher_letter(key, current):\n",
71 " new = (ord(current) - ord(key)) % 26\n",
72 " return chr(new + ord('a'))"
77 "execution_count": 13,
88 "execution_count": 13,
90 "output_type": "execute_result"
94 "encipher_letter('a', 'a')"
99 "execution_count": 14,
107 "'abcdefghijklmnopqrstuvwxyz'"
110 "execution_count": 14,
112 "output_type": "execute_result"
116 "''.join(encipher_letter('a', l) for l in string.ascii_lowercase)"
121 "execution_count": 15,
129 "'bcdefghijklmnopqrstuvwxyza'"
132 "execution_count": 15,
134 "output_type": "execute_result"
138 "''.join(encipher_letter('b', l) for l in string.ascii_lowercase)"
143 "execution_count": 16,
151 "'abcdefghijklmnopqrstuvwxyz'"
154 "execution_count": 16,
156 "output_type": "execute_result"
160 "''.join(decipher_letter('a', l) for l in string.ascii_lowercase)"
165 "execution_count": 17,
173 "'zabcdefghijklmnopqrstuvwxy'"
176 "execution_count": 17,
178 "output_type": "execute_result"
182 "''.join(decipher_letter('b', l) for l in string.ascii_lowercase)"
187 "execution_count": 18,
198 "execution_count": 18,
200 "output_type": "execute_result"
204 "decipher_letter('a', 'b')"
209 "execution_count": 19,
217 "'abcdefghijklmnopqrstuvwxyzbcdefghijklmnopqrstuvwxyzacdefghijklmnopqrstuvwxyzabdefghijklmnopqrstuvwxyzabcefghijklmnopqrstuvwxyzabcdfghijklmnopqrstuvwxyzabcdeghijklmnopqrstuvwxyzabcdefhijklmnopqrstuvwxyzabcdefgijklmnopqrstuvwxyzabcdefghjklmnopqrstuvwxyzabcdefghiklmnopqrstuvwxyzabcdefghijlmnopqrstuvwxyzabcdefghijkmnopqrstuvwxyzabcdefghijklnopqrstuvwxyzabcdefghijklmopqrstuvwxyzabcdefghijklmnpqrstuvwxyzabcdefghijklmnoqrstuvwxyzabcdefghijklmnoprstuvwxyzabcdefghijklmnopqstuvwxyzabcdefghijklmnopqrtuvwxyzabcdefghijklmnopqrsuvwxyzabcdefghijklmnopqrstvwxyzabcdefghijklmnopqrstuwxyzabcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvwyzabcdefghijklmnopqrstuvwxzabcdefghijklmnopqrstuvwxy'"
220 "execution_count": 19,
222 "output_type": "execute_result"
226 "''.join(encipher_letter(k, l) \n",
227 " for l in string.ascii_lowercase \n",
228 " for k in string.ascii_lowercase)"
233 "execution_count": 20,
241 "[('a', 'a', 'a'),\n",
242 " ('b', 'a', 'b'),\n",
243 " ('c', 'a', 'c'),\n",
244 " ('d', 'a', 'd'),\n",
245 " ('e', 'a', 'e'),\n",
246 " ('f', 'a', 'f'),\n",
247 " ('g', 'a', 'g'),\n",
248 " ('h', 'a', 'h'),\n",
249 " ('i', 'a', 'i'),\n",
253 "execution_count": 20,
255 "output_type": "execute_result"
259 "encrypted = [(k, l, encipher_letter(k, l)) \n",
260 " for l in string.ascii_lowercase \n",
261 " for k in string.ascii_lowercase]\n",
267 "execution_count": 21,
278 "execution_count": 21,
280 "output_type": "execute_result"
284 "all(l == decipher_letter(k, e) for k, l, e in encrypted)"
289 "execution_count": 32,
295 "def encipher_message(plaintext, keystring='key'):\n",
296 " key = itertools.cycle(keystring)\n",
297 " ciphertext = ''\n",
298 " previous = keystring[0]\n",
299 " for letter, keychar in zip(plaintext, key):\n",
300 " cipherletter = encipher_letter(keychar, letter)\n",
301 " ciphertext += cipherletter\n",
302 " previous = cipherletter\n",
303 " # previous = letter\n",
309 "execution_count": 23,
320 "execution_count": 23,
322 "output_type": "execute_result"
326 "encipher_message('hello', 'aardvark')"
331 "execution_count": 24,
342 "execution_count": 24,
344 "output_type": "execute_result"
348 "encipher_message('hello', 'q')"
353 "execution_count": 33,
359 "def decipher_message(ciphertext, keystring='key'):\n",
360 " key = itertools.cycle(keystring)\n",
362 " previous = keystring[0]\n",
363 " for letter, keychar in zip(ciphertext, key):\n",
364 " plainletter = decipher_letter(keychar, letter)\n",
365 " plaintext += plainletter\n",
366 " previous = letter\n",
367 " # previous = plainletter\n",
373 "execution_count": 27,
384 "execution_count": 27,
386 "output_type": "execute_result"
390 "decipher_message('hecoj', 'aardvark')"
395 "execution_count": 28,
406 "execution_count": 28,
408 "output_type": "execute_result"
412 "decipher_message('xubbe', 'q')"
417 "execution_count": 31,
428 "execution_count": 31,
430 "output_type": "execute_result"
434 "decipher_message('hecoj', 'q')"
439 "execution_count": 34,
447 "'rijvsmysmysmysmysmysmysmys'"
450 "execution_count": 34,
452 "output_type": "execute_result"
456 "encipher_message('helloooooooooooooooooooooo')"
461 "execution_count": 35,
469 "'helloooooooooooooooooooooo'"
472 "execution_count": 35,
474 "output_type": "execute_result"
478 "decipher_message('rijvsmysmysmysmysmysmysmys')"
483 "execution_count": 36,
491 "'qhiurlxrlxrlxrlxrlxrlxrlxr'"
494 "execution_count": 36,
496 "output_type": "execute_result"
500 "decipher_message('rijvsmysmysmysmysmysmysmys', 'b')"
505 "execution_count": 37,
513 "'dlghpjalicxktzjayi'"
516 "execution_count": 37,
518 "output_type": "execute_result"
522 "encipher_message(sanitised('This is a test message'), 'keyphrase')"
527 "execution_count": 38,
535 "'thisisatestmessage'"
538 "execution_count": 38,
540 "output_type": "execute_result"
544 "decipher_message('dlghpjalicxktzjayi', 'keyphrase')"
549 "execution_count": null,
559 "display_name": "Python 3",
560 "language": "python",
568 "file_extension": ".py",
569 "mimetype": "text/x-python",
571 "nbconvert_exporter": "python",
572 "pygments_lexer": "ipython3",