[Tutor] Columnar Transposition Cipher.

Tesla Coil tescoil@rtpro.net
Sun, 19 Mar 2000 17:36:15 -0600


The technique is described in opening paragraphs of:
http://www.cwi.nl/people/enigma/jsavard/pp0102.htm

The author has errored their example key, taking 'O'
for occurring alphabetically before 'N.'  The result
should have been HECRN CEYI ISEP SGDI RNTO
AAES RMPN SSRO EEBT ETIA EEHS.

Here's my little program to do it correctly.  I don't
know that this is the best approach, style is awful,
still lacking a decipher function, and telegraph should
output in five letter groups (HECRN CEYII SEPSG
DIRNT etc)--appreciate comments in these regards.

#coltrans.py - Columnar Transposition Cipher
import string
keystring = raw_input ('keyword please: ')
keylist = list(string.upper(keystring))
seqlist = []
idxlist=[]
messagestring = raw_input ('message please: ')
message = list(string.upper(messagestring))
print ' '

def unstray(listinput):
    for stray in listinput:
        if stray not in string.uppercase:
            listinput.remove(stray)

def keysequence(alphakey):
    for letter in range(len(string.uppercase)):
        for item in range(len(alphakey)):
            if string.uppercase[letter]==alphakey[item]:
                seqlist.append(item)

def transequence(numberkey):
    for number in range(len(numberkey)):
        idxpos = numberkey[number]
        idxlist.append(idxpos)
            while idxpos+len(numberkey)<= (len(message)-1):
                idxpos = idxpos+len(numberkey)
                idxlist.append(idxpos)

def transpose(transkey):
    for number in range(len(transkey)):
        transkey[number]=message[transkey[number]]

def telegraph(output):
    for number in range(len(output)):
        print output[number],

def encipher():
    unstray(keylist)
    unstray(message)
    keysequence(keylist)
    transequence(seqlist)
    transpose(idxlist)
    telegraph(idxlist)

encipher()