4 "signature": "sha256:c4879f550d49d776eff31ee9f3296756aab1dcd81e1e5aad61ade46d9b29600d"
15 "from cipher import *"
26 "railfence_encipherl('hellothere', 3)"
33 "output_type": "pyout",
46 "message = 'hellothere'\n",
58 "sections = chunks(message, (height - 1) * 2, fillvalue='!')\n",
66 "output_type": "pyout",
69 "['he', 'll', 'ot', 'he', 're']"
79 "rows = [s[0] for s in sections]\n",
87 "output_type": "pyout",
90 "['h', 'l', 'o', 'h', 'r']"
100 "for r in range(1, height - 1):\n",
101 " rows += [s[r] + s[-r] for s in sections]\n",
104 "language": "python",
109 "output_type": "pyout",
110 "prompt_number": 172,
112 "['h', 'l', 'o', 'h', 'r']"
122 "rows += [s[height - 1] for s in sections]\n",
125 "language": "python",
130 "output_type": "pyout",
131 "prompt_number": 173,
133 "['h', 'l', 'o', 'h', 'r', 'e', 'l', 't', 'e', 'e']"
143 "ciphertext = ''.join(rows)\n",
146 "language": "python",
151 "output_type": "pyout",
152 "prompt_number": 174,
164 "def railfence_encipherl(message, height, fillvalue=' '):\n",
165 " \"\"\"Railfence cipher\n",
167 " sections = chunks(message, (height - 1) * 2, fillvalue=fillvalue)\n",
168 " # Add the top row\n",
169 " rows = [s[0] for s in sections]\n",
170 " # process the middle rows of the grid\n",
171 " for r in range(1, height - 1):\n",
172 " rows += [s[r] + s[-r] for s in sections]\n",
173 " # process the bottom row\n",
174 " rows += [s[height - 1] for s in sections]\n",
175 " return ''.join(rows)"
177 "language": "python",
186 "ciphertext = railfence_encipherl(message, height, fillvalue='!')\n",
189 "language": "python",
194 "output_type": "pyout",
195 "prompt_number": 175,
209 "language": "python",
214 "output_type": "pyout",
215 "prompt_number": 176,
227 "n_secs = len(ciphertext) // ((height - 1) * 2)\n",
230 "language": "python",
235 "output_type": "pyout",
236 "prompt_number": 177,
250 "language": "python",
255 "output_type": "pyout",
256 "prompt_number": 109,
270 "language": "python",
275 "output_type": "pyout",
276 "prompt_number": 110,
290 "language": "python",
295 "output_type": "pyout",
296 "prompt_number": 111,
310 "language": "python",
315 "output_type": "pyout",
316 "prompt_number": 112,
328 "downrows = [ciphertext[:n_secs]]\n",
332 "language": "python",
337 "output_type": "pyout",
338 "prompt_number": 180,
350 "for r in range(1, height, 2):\n",
351 " midrow = ciphertext[r * n_secs:(r+2)*n_secs]\n",
352 " downrows += [''.join([midrow[i] for i in range(0, len(midrow), 2)])]\n",
353 " uprows = [''.join([midrow[i] for i in range(1, len(midrow), 2)])] + uprows\n",
356 "language": "python",
361 "output_type": "pyout",
362 "prompt_number": 179,
364 "(['hlohr', 'ete'], ['le'])"
374 "downrows += [ciphertext[-n_secs:]]\n",
377 "language": "python",
382 "output_type": "pyout",
383 "prompt_number": 181,
385 "(['hlohr', 'eltee'], [])"
395 "rows = downrows + uprows\n",
398 "language": "python",
403 "output_type": "pyout",
404 "prompt_number": 182,
416 "midrow = rows[1]\n",
419 "language": "python",
424 "output_type": "pyout",
437 "mid_down = ''.join([midrow[i] for i in range(0, len(midrow), 2)])\n",
440 "language": "python",
445 "output_type": "pyout",
458 "mid_up = ''.join([midrow[i] for i in range(1, len(midrow), 2)])\n",
461 "language": "python",
466 "output_type": "pyout",
479 "rows = downrows + uprows\n",
482 "language": "python",
487 "output_type": "pyout",
488 "prompt_number": 139,
490 "['hhaesspfoilcs',\n",
491 " 'eesataitrnfi!',\n",
492 " 'lrtrhleetgep!',\n",
493 " 'lemtiocxernh!',\n",
494 " 'oaeisnetsace!',\n",
505 "''.join(letter for section in zip(*rows) for letter in section)"
507 "language": "python",
512 "output_type": "pyout",
513 "prompt_number": 183,
525 "def railfence_decipherl(message, height):\n",
526 " n_secs = len(message) // ((height - 1) * 2)\n",
527 " downrows = [message[:n_secs]]\n",
529 " for r in range(height-2):\n",
530 " midrow = ciphertext[(2 * r + 1) * n_secs:(2 * r + 1) * n_secs + n_secs * 2]\n",
531 " downrows += [''.join([midrow[i] for i in range(0, len(midrow), 2)])]\n",
532 " uprows = [''.join([midrow[i] for i in range(1, len(midrow), 2)])] + uprows\n",
533 " downrows += [message[-n_secs:]]\n",
534 " rows = downrows + uprows\n",
535 " return ''.join(letter for section in zip(*rows) for letter in section)"
537 "language": "python",
548 "language": "python",
553 "output_type": "pyout",
554 "prompt_number": 154,
556 "'hhaesetevshae!!lorateri!!lemt!'"
566 "railfence_decipherl(ciphertext, 4)"
568 "language": "python",
573 "output_type": "pyout",
574 "prompt_number": 155,
586 "plaintext = sanitise('hello there avast me hearties this is a long piece of text for testing railfence ciphers')\n",
589 "language": "python",
594 "output_type": "pyout",
595 "prompt_number": 250,
597 "'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers'"
607 "ciphertext = railfence_encipherl(plaintext, height, fillvalue='!')\n",
610 "language": "python",
615 "output_type": "pyout",
616 "prompt_number": 163,
618 "'hhaesspfoilcsetevshaetiagiotfrtnifeir!!loraterihslneeettsgaecpe!!lemtiocxernh!'"
628 "railfence_decipherl(ciphertext, height)"
630 "language": "python",
635 "output_type": "pyout",
636 "prompt_number": 164,
638 "'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers!!!!!'"
648 "railfence_encipherl('hellothere', 2)"
650 "language": "python",
655 "output_type": "pyout",
656 "prompt_number": 186,
668 "railfence_decipherl('hlohreltee', 2)"
670 "language": "python",
675 "output_type": "pyout",
676 "prompt_number": 187,
688 "[i for i in range(1, 2, 2)]"
690 "language": "python",
695 "output_type": "pyout",
696 "prompt_number": 184,
708 "railfence_encipherl('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 10, fillvalue='!')"
710 "language": "python",
715 "output_type": "pyout",
716 "prompt_number": 265,
718 "'hepisehagitnr!!lernesge!!lmtocerh!!otiletap!!tseaorii!!hassfolc!!evtitffe!!rahsetec!!eixn!'"
728 "railfence_decipherl('horaersslpeeosglcpselteevsmhatetiiaogicotxfretnrifneihr!!lhateihsnefttiaece!', 3)"
730 "language": "python",
735 "output_type": "pyout",
736 "prompt_number": 191,
738 "'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers!!!'"
748 "railfence_encipherl('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 4, fillvalue='!')"
750 "language": "python",
755 "output_type": "pyout",
756 "prompt_number": 240,
758 "'hhaesspfoilcsetevshaetiagiotfrtnifeir!!loraterihslneeettsgaecpe!!lemtiocxernh!'"
768 "railfence_decipherl('hhaesspfoilcsetevshaetiagiotfrtnifeir!!loraterihslneeettsgaecpe!!lemtiocxernh!', 4)"
770 "language": "python",
775 "output_type": "pyout",
776 "prompt_number": 241,
778 "'hmelfhhetettaaimaoecetcosfei!rsn!olrpntcvefrsxa!o!teilihirgaltintecixhehsst!in'"
788 "railfence_encipherl('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 5, fillvalue='!')"
790 "language": "python",
795 "output_type": "pyout",
796 "prompt_number": 238,
798 "'hresleogcseeemhetaocofrnrner!!lhateihsnefttiaece!!ltvsatiigitxetifih!!oarspeslp!'"
808 "railfence_decipherl('hresleogcseeemhetaocofrnrner!!lhateihsnefttiaece!!ltvsatiigitxetifih!!oarspeslp!', 5)"
810 "language": "python",
815 "output_type": "pyout",
816 "prompt_number": 239,
818 "'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers!!!!!!!'"
828 "len(railfence_encipherl('hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers', 5, fillvalue='!'))"
830 "language": "python",
835 "output_type": "pyout",
836 "prompt_number": 199,
848 "ciphertext = 'horaersslpeeosglcpselteevsmhatetiiaogicotxfretnrifneihr!!lhateihsnefttiaece!'\n",
851 "language": "python",
860 "ciphertext = 'hhaesspfoilcsetevshaetiagiotfrtnifeir!!loraterihslneeettsgaecpe!!lemtiocxernh!'\n",
863 "language": "python",
872 "ciphertext = 'hresleogcseeemhetaocofrnrner!!lhateihsnefttiaece!!ltvsatiigitxetifih!!oarspeslp!'\n",
875 "language": "python",
884 "n_secs = len(ciphertext) // ((height - 1) * 2)\n",
887 "language": "python",
892 "output_type": "pyout",
893 "prompt_number": 243,
905 "downrows = [ciphertext[:n_secs]]\n",
909 "language": "python",
914 "output_type": "pyout",
915 "prompt_number": 244,
917 "(['hhaesspfoilcs'], [])"
927 "for r in range(height-2):\n",
928 " print('From', (2 * r + 1) * n_secs, 'to', (2 * r + 1) * n_secs + n_secs * 2)\n",
929 " midrow = ciphertext[(2 * r + 1) * n_secs:(2 * r + 1) * n_secs + n_secs * 2]\n",
930 " downrows += [''.join([midrow[i] for i in range(0, len(midrow), 2)])]\n",
931 " uprows = [''.join([midrow[i] for i in range(1, len(midrow), 2)])] + uprows\n",
934 "language": "python",
938 "output_type": "stream",
947 "output_type": "pyout",
948 "prompt_number": 245,
950 "(['hhaesspfoilcs', 'eesataitrnfi!', 'lrtrhleetgep!'],\n",
951 " ['oaeisnetsace!', 'tvheigoftier!'])"
961 "downrows += [ciphertext[-n_secs:]]\n",
964 "language": "python",
969 "output_type": "pyout",
970 "prompt_number": 246,
972 "(['hhaesspfoilcs', 'eesataitrnfi!', 'lrtrhleetgep!', 'lemtiocxernh!'],\n",
973 " ['oaeisnetsace!', 'tvheigoftier!'])"
983 "rows = downrows + uprows"
985 "language": "python",
994 "''.join(letter for section in zip(*rows) for letter in section)"
996 "language": "python",
1001 "output_type": "pyout",
1002 "prompt_number": 248,
1004 "'hellothereavastmeheartiesthisisalongpieceoftextfortestingrailfenceciphers!!!!!'"
1008 "prompt_number": 248
1011 "cell_type": "code",
1014 "[i for i in range(0)]"
1016 "language": "python",
1021 "output_type": "pyout",
1022 "prompt_number": 236,
1028 "prompt_number": 236
1031 "cell_type": "code",
1035 "ciphertext = railfence_encipherl(plaintext, height, fillvalue=' ')\n",
1036 "result = railfence_decipherl(ciphertext, height).strip()\n",
1037 "result == plaintext"
1039 "language": "python",
1044 "output_type": "pyout",
1045 "prompt_number": 270,
1051 "prompt_number": 270
1054 "cell_type": "code",
1059 "language": "python",
1064 "output_type": "pyout",
1065 "prompt_number": 271,
1067 "'horaersslpeeosglcpselteevsmhatetiiaogicotxfretnrifneihr lhateihsnefttiaece '"
1071 "prompt_number": 271
1074 "cell_type": "code",
1077 "language": "python",