Lab 6-3: Bruteforcing the Transposition Cipher

Note: Part of this lab came from Al Sweigart’s great book, Hacking Secret Ciphers with Python: A beginner’s Guide to cryptography and computer programming with Python, available online here at Invent With Python, among his other works. Feel free to check them out if they interest you!

Lab Requirements and Specifications

Similar to the Caesar cipher bruteforcing, you are to try every possible key for the transposition cipher. Unlike the Caesar cipher where there are only 26 possible keys, the number of possible keys in the transposition cipher is not a set number. Instead, the number of possible keys is dependent on the length of the message. A message with 150 characters can have 150 keys. This makes it kind of a chore to brute force it and check each decryption manually, but that’s what our program is going to do.

You should name your file FILN_brutetransposition.py, where FILN is your first initial and last name, no space.

Testing Your Program

You can test your program by encrypting text with your transposition.py module from before, and then bruteforcing it to make sure that the key is accurate. As a challenge, I also offer you a bit of text to decrypt. Try to find the key to this one!

HDI ee olcmrlea omym,b a enyIrob  tea1  m4ba tedch vu,air  crIwee’a,nml t rjlmuuyoss v t1iy 5nei gtny ,bet oaobxr u sAtm n eotI.la  drpT trhaioancmndaik ;s I ey h owoyuamo neuat  l oiltff o  s tyobho eeumc  uoggcmriheev  ae~at ~ emrwsenat ul aGrw fuacVslhZ.raq unbIscA eeJks Pn.aCo nxwIdG ’ Utvtzhehke eyra pelsS’rulsepf apAadou yrkm tKic RluITlt ai nLooeTnfef fdRp ,Hem aoyIEp  jlawcerig mlQoslJu, yt bI aertnczhdoYe mmrneleo w wtnj hmuseYsl Itigr drjleeHi aVkotHeneC  sGmmtVey D, wn saLbtlSuorbtmuX asTIc X help vereeBovrDme.Sir SsyIFewfg h xyeyForoiueuM   sIIhK’ aXmgvj oeud  uiaazfsnDf yQet KrrqHeaupnieutnsf.itQ nijOgoen.ne  sW

Below is a sample output of what a program execution might look like, using our encrypted message from before:

"aeeeiywvyhhil  nnoiicoevwagdtuloh reasusarllokeay y i  epn dsi uncbnaoys fwpioettwogc h nd  hsusotomgeap   hdh a   swwlk"

And as we run it through our Transposition Cipher bruteforcer (showing only the first 55 characters):

Trying key #1: aeeeiywvyhhil  nnoiicoevwagdtuloh reasusarllokeay y i  ...
Trying key #2: aseie euinycwbvnyahohyisl  f wnpniooieitctoweovgwca ghd...
Trying key #3: aaterweloelgiocyk wehva yynh dhy i  lih  s  unesnpoonti...
Trying key #4: als eoi eh he usirnuyecswabovsntyuaohsomhaygirsell a lf...
Trying key #5: awy oea ftegywoed pmitiigyu oewl eavoetpyhpt h nw hr o ...
Trying key #6: acastgeoriweeel oaevlugpiwonc yakc  wgebh vdan hytyandh...
Trying key #7: aia   deisef hecupwh eosnpsaiea iu yvrdos wwlseo valitt...
Trying key #8: anlksp  enoeii  eoha ohhei yuesdiir ntuhyceycts woa bwo...
Trying key #9: a tlpyco enulns m enlo  hg eookdf esiiheswnawyi aipdpww...
Trying key #10: always code as if the guy who ends up maintaining your ...
Trying key #11: aie oebonm elvrkpnedgae weenat e e aaa ot a ingsydywhp ...
Trying key #12: ahclaysyt g eioor isw eaelehli  oha e v l ufgsp i wro n...
Trying key #13: ahclaysyt g eioor isw eaelehli  oha e v l ufgsp i wro n...
Trying key #14: ahidak u w mdwehitseenfo ghleicuuapcwghe kelolsynbpcsaa...
Trying key #15: aynwhaypn tno  ehoa r ncfwdt  ehigrly bwo o  eiidel dnp...
Trying key #16: aynwhaypn tno  ehoa r ncfwdt  ehigrly bwo o  eiidel dnp...
Trying key #17: aynwhaypn tno  ehoa r ncfwdt  ehigrly bwo o  eiidel dnp...
Trying key #18: av otel puyoc o  keyneualynnse hm  ehnvlso  c thsgh eho...
Trying key #19: av otel puyoc o  keyneualynnse hm  ehnvlso  c thsgh eho...
Trying key #20: awliwlaly sb ec oad ev iaosl einft  tphsey cghuoyp awth...
Trying key #21: awliwlaly sb ec oad ev iaosl einft  tphsey cghuoyp awth...
Trying key #22: awliwlaly sb ec oad ev iaosl einft  tphsey cghuoyp awth...
Trying key #23: awliwlaly sb ec oad ev iaosl einft  tphsey cghuoyp awth...
Trying key #24: ayhncaleakyescypt  sg  sewinogoare pibsiwh oe awevloedh...
Trying key #25: ayhncaleakyescypt  sg  sewinogoare pibsiwh oe awevloedh...
Trying key #26: ayhncaleakyescypt  sg  sewinogoare pibsiwh oe awevloedh...
Trying key #27: ayhncaleakyescypt  sg  sewinogoare pibsiwh oe awevloedh...
Trying key #28: ayhncaleakyescypt  sg  sewinogoare pibsiwh oe awevloedh...
Trying key #29: ayhncaleakyescypt  sg  sewinogoare pibsiwh oe awevloedh...
Trying key #30: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #31: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #32: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #33: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #34: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #35: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #36: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #37: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #38: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #39: aiylncwthaaoyipsna itcnhog d weyh ooau srk  nicofow dst...
Trying key #40: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #41: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #42: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #43: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #44: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #45: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #46: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #47: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #48: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #49: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #50: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #51: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #52: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #53: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #54: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #55: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #56: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #57: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #58: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #59: aewhlniowdl aslky  nsubo pewc  soma da weivh nieatorsal...
Trying key #60: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #61: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #62: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #63: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #64: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #65: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #66: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #67: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #68: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #69: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #70: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #71: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #72: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #73: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #74: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #75: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #76: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #77: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #78: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #79: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #80: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #81: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #82: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #83: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #84: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #85: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #86: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #87: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #88: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #89: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #90: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #91: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #92: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #93: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #94: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #95: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #96: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #97: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #98: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #99: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #100: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #101: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #102: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #103: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #104: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #105: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #106: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #107: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #108: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #109: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #110: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #111: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #112: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #113: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #114: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #115: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #116: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #117: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #118: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...
Trying key #119: aeiwyhl nicewgtlhraualoeyyi p s nbay wietochn huooga  d...

(In case you’re wondering why we get so many repeats, it’s because we use math.ceil() in our decryption algorithm, which rounds numbers up when we calculate the # of rows - meaning we will have similar decrypted messages for keys of higher numbers.)

The following space is provided in case you want to test code out or write it in the browser:

Next Section - Lesson 07-01: Dictionaries