[Edu-sig] Cryptonomicon
Kirby Urner
pdx4d@teleport.com
Sun, 19 Nov 2000 09:20:12 -0800
Earlier I wrote:
>Plus just consider those really simple substitution codes where
>you make A = J, B = Z -- whatever random assignment. Also,
>you want to parse your messages into 5 letter chunks: ALSOY
>OUWAN TOPAR SEYOU RMESSA GESIN TO5LE TTERC HUNKS. You find
>this simple "club house" codes in books for little kids, with
>titles like 'I, Spy' and stuff. Of course this is nothing
>sophisticated, but it's a great opportunity to use Python
>string ops to: make the 5 letter uppercase chunks out of
>whatever plaintext input (could be interactive and first,
>then convert to file i/o); use the dictionary data structure
>to do the lookup/substition.
I've played around with the above, so far minus the 5-letter
chunking, to develop a simple "clubhouse" substitution scheme
in Python.
Here's the kind of file it produces:
RUFWVEUWB PLS VBJBL QBPWV PAU UFW RPTGBWV HWUFAGT RUWTG
UL TGNV EULTNLBLT P LBY LPTNUL, EULEBNJBS NL CNHBWTQ PLS
SBSNEPTBS TU TGB OWUOUVNTNUL TGPT PCC MBL PWB EWBPTBS
BIFPC. LUY YB PWB BLAPABS NL P AWBPT ENJNC YPW, TBVTNLA
YGBTGBW TGPT LPTNUL UW PLQ LPTNUL VU EULEBNJBS PLS VU
SBSNEPTBS EPL CULA BLSFWB. YB PWB MBT UL P AWBPT HPTTCBRNBCS
UR TGPT YPW. YB GPJB EUMB TU SBSNEPTB P OUWTNUL UR TGPT RNBCS
PV P RNLPC WBVTNLA-OCPEB RUW TGUVB YGU GBWB APJB TGBNW CNJBV
TGPT TGPT LPTNUL MNAGT CNJB. NT NV PCTUABTGBW RNTTNLA PLS
OWUOBW TGPT YB VGUFCS SU TGNV. HFT NL P CPWABW VBLVB, YB
EPLLUT SBSNEPTB, YB EPLLUT EULVBEWPTB, YB EPLLUT GPCCUY TGNV
AWUFLS. TGB HWPJB MBL, CNJNLA PLS SBPS YGU VTWFAACBS GBWB
GPJB EULVBEWPTBS NT RPW PHUJB UFW OUUW OUYBW TU PSS UW SBTWPET.
TGB YUWCS YNCC CNTTCB LUTB LUW CULA WBMBMHBW YGPT YB VPQ GBWB,
HFT NT EPL LBJBW RUWABT YGPT TGBQ SNS GBWB.
... along with the all-important key file, saved separately:
D=Z
Z=X
Q=Y
J=V
...
and so on -- all 26 letters have their random substitutes.
When you bring the key file and encrypted text back together,
the original text (except capitalized).
Notice: this primitive scheme keeps punctuation and whitespace as
is, simply substitutes for the 26 uppercase alpha letters. But
it's still a useful exercise for learning various aspects of
Python, as well as starting to think like a cryptographer.
For example, coming up with a substitution key is automatic
and involves pseudo-randomly choosing from a grab-bag of
alpha characters, removing the chosen letter, and choosing
again, until all 26 have been chosen at pseudo-random. You
then build a dictionary with this, i.e. pair 'A' with the
first letter chose, 'B' with the next, and so on. There's
no rule which says you can't pair a letter with itself --
could happen. Here's some code:
import string, random
def permute():
"""
Randomly permute the uppercase alphabet
by choosing its letters pseudo-randomly
"""
alphalist = list(string.uppercase)
newlist = []
for i in range(len(alphalist)):
randchar = random.choice(alphalist)
alphalist.remove(randchar)
newlist.append(randchar)
return newlist
def mkdict():
"""
Pair uppercase alphabet with randomly permuted
version of same
"""
tuples = zip(string.uppercase,permute())
codedict = {}
for pair in tuples:
codedict[pair[0]]=pair[1]
return codedict
The rest of clubhouse.py is about file i/o. You feed it
filename.txt and get back filename.cpt and filename.key,
the encrypted text and deciphering key. Then you reverse
the process, getting filename.dcp (decrypted) back out.
Looks like this in IDLE:
>>> clubhouse.encrypt(r"./ocn/sample.txt")
Writing to ./ocn/sample.cpt
Saving key as ./ocn/sample.key
>>> clubhouse.decrypt(r"./ocn/sample.cpt")
Reading from ./ocn/sample.cpt
Writing to ./ocn/sample.dcp
Using key ./ocn/sample.key
FOURSCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH
ON THIS CONTINENT A NEW NATION, CONCEIVED IN LIBERTY AND
DEDICATED TO THE PROPOSITION THAT ALL MEN ARE CREATED
EQUAL. NOW WE ARE ENGAGED IN A GREAT CIVIL WAR, TESTING
WHETHER THAT NATION OR ANY NATION SO CONCEIVED AND SO
DEDICATED CAN LONG ENDURE. WE ARE MET ON A GREAT BATTLEFIELD
OF THAT WAR. WE HAVE COME TO DEDICATE A PORTION OF THAT FIELD
AS A FINAL RESTING-PLACE FOR THOSE WHO HERE GAVE THEIR LIVES
THAT THAT NATION MIGHT LIVE. IT IS ALTOGETHER FITTING AND
PROPER THAT WE SHOULD DO THIS. BUT IN A LARGER SENSE, WE
CANNOT DEDICATE, WE CANNOT CONSECRATE, WE CANNOT HALLOW THIS
GROUND. THE BRAVE MEN, LIVING AND DEAD WHO STRUGGLED HERE
HAVE CONSECRATED IT FAR ABOVE OUR POOR POWER TO ADD OR DETRACT.
THE WORLD WILL LITTLE NOTE NOR LONG REMEMBER WHAT WE SAY HERE,
BUT IT CAN NEVER FORGET WHAT THEY DID HERE.
I've put the colorized (HTMLized) code for my clubhouse.py
(version 1.0) at
http://www.inetarena.com/~pdx4d/ocn/clubhouse.html
with non-HTMLized (plaintext) source code at:
http://www.inetarena.com/~pdx4d/ocn/python/clubhouse.py
The html file has tie-backs to some of our posts in this
thread on edu-sig (including this one).
Kirby