Stupid Python Trick #4: Classical Cryptography

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Stupid Python Trick #4: Classical Cryptography

Postby ichabod801 » Sat Feb 09, 2013 8:33 pm

When I was a kid, I was into codes. I made one with the Greek alphabet and used to write myself secret notes all the time. When I had to write a five page paper in fifth grade, I wrote it on codes. So for this Stupid Python Trick, I decided to do cryptography. Not modern cryptography, but the classical cryptography that I was into as a kid. So I went through the classical cryptography section in Wikipedia and wrote Python code for just about everything there. It's got everything from ancient ciphers like Caesar and Scytale to cold war ciphers like VIC and Chao.

The code is at http://www.xenomind.com/Python/cipher.py. I wanted to play around with callable objects, so every code/cipher in the program is done as an object with a __call__ method, and an instance of each object is provided in the module. Note that the program as is will try to run a full test of every CipherFunction object. This will fail because the Book CipherFunction is expecting to have a copy of the Bible handy. There are many ways to fix this:

  • Comment out the call to full_test at the bottom of the file.
  • Download the copy of the Bible it is expecting and unzip it the same folder (http://www.xenomind.com/Python/AV_txt.zip)
  • Modify the Book object to point to a different large text file with lots of words in it.
  • Comment out the Book object.

Below is the output of the full as run on my machine. It runs every cipher against a pangram I made up for the test. If there was an example on Wikipedia, it also enciphers and deciphers that, which allows me to check the enciphering in isolation. The numbers in parentheses after the test outputs are the difflib module text difference measures between the actual and expected output.

cipher.py wrote:ADFGX Default:
CIPHER: FAXDFADDDGDGFFFAFAXAAFAFX (0.96)
PLAIN: ATTACKATONCE (0.923)

ADFGX Requested:
CIPHER: GXFAFGAXXGAXXFDFXFGDGDGFAFXADADGGFGFGFGDAAADFDFDGAFDAGDAXAFDAGGXXDGFDAAG
AGDXXGFDGGXAAXAFGF
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBIAVALAZYLEARNINGB (0.878)

Affine Default:
CIPHER: Ihhwvc swfrcp (1.0)
PLAIN: Affine cipher (1.0)

Affine Requested:
CIPHER: Owly Fyzrav kewsg uapz hwtcu xeqn Biji lidy lcipvwvm.
PLAIN: Wily Python quick sort fixes dumb Java lazy learning. (1.0)

Atbash Requested:
CIPHER: Drob Kbgslm jfrxp hlig urcvh wfny Qzez ozab ovzimrmt.
PLAIN: Wily Python quick sort fixes dumb Java lazy learning. (1.0)

AutoACA Requested:
CIPHER: LPWC VKPPZL FSBJY FELB HSPSJ WZUY NSYU XBIY GELRMGYK.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Bifid Default:
CIPHER: UAEOLWRINS (1.0)
PLAIN: FLEEATONCE (0.909)

Bifid Requested:
CIPHER: WPPRNTFIORKDDLFVLZLDMMIDYSSESXRQSXUGQAAYEBOM
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBIAVALAZYLEARNING (0.887)

Book Requested:
CIPHER: 2 69 2 2 73 5 222 35 26 0 61 249 228 39 7 208 66 8 262 89 19 93 42 12 2
69 2 2 67 7 314 63 11 99 13 12 45 41 15 235 42 10 101 84 16 321 91 16 93 46 20 2
62 89 19 326 1 16 3 65 37 329 44 3 2 73 24 207 86 6 12 93 3 111 41 3 129 59 35 2
62 89 19 2 73 24 333 19 8 2 73 24 2 67 9 0 37 647 309 4 14 257 62 10 253 15 13 9
3 42 12 2 67 7 262 89 19 80 29 35 36 3 5 114 49 6 124 59 18 4 81 12 2 67 7 278 6
0 5 308 71 11
PLAIN: WILY PYTHON QUICK SORT FIES DUMB JAVA LAZY LEARNING (0.981)

Caesar Default:
CIPHER: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ. (1.0)
PLAIN: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. (1.0)

Caesar Requested:
CIPHER: ZLOB SBWKRQ TXLFN VRUW ILAHV GXPE MDYD ODCB OHDUQLQJ.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Chinese Default:
CIPHER: RRGTAAOHFNDE (1.0)
PLAIN: THEDOGRANFAR (1.0)

Chinese Requested:
CIPHER: Xnryzvaudifs q yPW.ia aaJm x okunt ignell bsetrciohyl
PLAIN: Wily Python quick sort fixes dumb Java lazy learning.X (0.991)

Columnar Default:
CIPHER: EVLNEACDTKESEAQROFOJDEECUWIREE (1.0)
PLAIN: WEAREDISCOVEREDFLEEATONCEQKJEU (0.806)

Columnar Requested:
CIPHER: YNSXBARXPQOEJZNJIHCFUAEGLOKIMLAQYURSAYIZWTITDVLN
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBJAVALAZYLEARNINGQXJZ (0.871)

Delastelle Requested:
CIPHER: 322133231313213313321123232222223331133131221311232233321132133332122311
112331212121211111312111231111323313231122111233222133222113333
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBJAVALAZYLEARNING. (0.918)

Dvorak Requested:
CIPHER: <gpt Rtkjsl xfgiv ;sok ygbd; hfmn Ca.a pa/t pdaolglue
PLAIN: Wily Python quick sort fixes dumb Java lazy learning. (1.0)

FourSquare Requested:
CIPHER: YGNW TVSINO QTJAM UMTR IFZCT CSOE GAVB KEVW NAETLING.R
PLAIN: WILY PYTHON UICK SORT FIXES DUMB JAVA LAZY LEARNING.R (0.981)

Morse Requested:
CIPHER: *-- ** *-** -*-- *--* -*-- - **** --- -* --*- **- ** -*-* -*-
*** --- *-* - **-* ** -**- * *** -** **- -- -*** *--- *- ***- *-
*-** *- --** -*-- *-** * *- *-* -* ** -* --* *-*-*-
PLAIN: wily python quick sort fixes dumb java lazy learning. (1.0)

Mutating Requested:
CIPHER: WLRH BNLCMO UBSPA LKQV KQISJ XRME PJHP DVXZ PLKEDBJF.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Nihilist Default:
CIPHER: 37 106 62 36 67 47 86 26 104 53 62 77 27 55 57 66 55 36 54 27 (1.0)
PLAIN: DYNAMITEWINTERPALACE (0.952)

Nihilist Requested:
CIPHER: 32 47 7 14 2 31 24 46 10 48 8 22 4 36 1 3 1 19 24 44 0 13 37 20 49 13 8
27 46 43 31 34 7 26 22 31 11 38 42 2 0 1 13 45
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBIAVALAYLEARNING (0.875)

Number Requested:
CIPHER: 23-9-12-25 16-25-20-8-15-14 17-21-9-3-11 19-15-18-20 6-9-24-5-19 4-21-13
-2 10-1-22-1 12-1-26-25 12-5-1-18-14-9-14-7
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING (0.99)

Polybius Requested:
CIPHER: 522431543554442334334145241325433442442124531543144532122411511131115554
3115114233243322
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBIAVALAZYLEARNING (0.887)

RailFence Default:
CIPHER: WECRLTEERDSOEEFEAOCAIVDEN (1.0)
PLAIN: WEAREDISCOVEREDFLEEATONCE (0.877)

RailFence Requested:
CIPHER: WPOIOIDJLLNIYYHNUCSRFXSUBAAAYERIGLTQKTEMVZAN
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBJAVALAZYLEARNING (0.907)

Reflexive Requested:
CIPHER: JVYL CLGUBA DHVPX FBEG SVKRF QHZO WNIN YNML YRNEAVAT.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

OneTimePad Requested:
CIPHER: GBFS!9Lh8k(>gO!Jx%u>71kukK"3!^wy"Q$$.A4m |H#2CZ#kYpTz
PLAIN: Wily Python quick sort fixes dumb Java lazy learning. (1.0)

Playfair Requested:
CIPHER: YGOV OZSJTS VZHDH UMTQ JHYCU ETNC FDWB MBED PABQOHMH.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB IAVA LAZY LEARNING. (0.981)

Solitaire Default:
CIPHER: KIRAKSFJAN (1.0)
PLAIN: SOLITAIREX (1.0)

Solitaire Requested:
CIPHER: DDVHCFIBLDNHYUUXPVRHDRWMNAIARULGKTRKQJTBHHYK
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBJAVALAZYLEARNING (0.907)

Straddling Default:
CIPHER: 3113212731223655 (1.0)
PLAIN: ATTACKATDAWN (0.923)

Straddling Requested:
CIPHER: 543185659505661574595152311175927965913355825912521910591605305918057565
9188094341458
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Substitution Requested:
CIPHER: MOCU BUNSIT ZYOLV HIDN GOJAH RYWP XEKE CEQU CAEDTOTF.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Trifid Requested:
CIPHER: VZVQDWSYFBVIJKPRAQQXIRTCJGBTZPL.FLSHHKMAJIBZR
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBJAVALAZYLEARNING. (0.918)

TwoSquare Requested:
CIPHER: WIMZ MVSGNM QUHBH QORU GHWCQ AROD KBZE PEWV LEDUMHOH.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB IAVA LAZY LEARNING. (0.981)

Scytale Default:
CIPHER: HENTEIDTLAEAPMRCMUAK (1.0)
PLAIN: HELPMEIAMUNDERATTACK (0.87)

Scytale Requested:
CIPHER: WSVIOALRLYTAPFZYIYTXLHEEOSANDRQUNUMIIBNCJGKAQ
PLAIN: WILYPYTHONQUICKSORTFIXESDUMBJAVALAZYLEARNINGQ (0.898)

AutoBellaso Requested:
CIPHER: JWNI CKEZKA DGXSO FACD VZIXB QGOR ZRGT PSKI ZTQBJVMV.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Chao Default:
CIPHER: OAHQ HCNY NX TSZJRR HJBY HQKS OUJY. (1.0)
PLAIN: WELL DONE IS BETTER THAN WELL SAID. (1.0)

Chao Requested:
CIPHER: WHXV LINASG HKYAX AKCO CRZGI OHNC RGEJ QEDS YOCFSTRP.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Sequence Requested:
CIPHER: JWNI LLFWLJ DGXSO FACD WVJTC UHNQ ZRIO NQIL ZTQAAWLW.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Vinegere Default:
CIPHER: LXFOPVEFRNHR (1.0)
PLAIN: ATTACKATDAWN (1.0)

Vinegere Requested:
CIPHER: LPWC VKIOZR WGXJV WUDI MTBKE SBXF PMKH WEFK ALLVTUCN.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)

Keyword Requested:
CIPHER: WCIY OYTBNK QUCLF SNRT MCXGS EUJH DPVP IPZY IGPRKCKA.
PLAIN: WILY PYTHON QUICK SORT FIXES DUMB JAVA LAZY LEARNING. (1.0)



32 ciphers tested.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 93
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 1 guest