[Tutor] Columnar Transposition Cipher.
Tesla Coil
tescoil@rtpro.net
Tue, 21 Mar 2000 11:53:43 -0600
20 Mar 2000, Justin Sheehy wrote:
> You don't comment the code, so it is hard to tell your intent.
> If this function is intended to provide the column numbers
> when given the key word, it is incorrect. If the keyword is
> 'CONVENIENCE', the column numbers should be:
>
> [1, 10, 7, 11, 3, 8, 6, 4, 9, 2, 5]
>
> Your function provides:
>
> [0, 9, 4, 7, 10, 6, 2, 5, 8, 1, 3]
The two lists express the same thing from different angles.
pencilkey=[1, 10, 7, 11, 3, 8, 6, 4, 9, 2, 5]
indexkey=[0, 9, 4, 7, 10, 6, 2, 5, 8, 1, 3]
for n in range(len(indexkey)):
print pencilkey[indexkey[n]],
Will return: 1 2 3 4 5 6 7 8 9 10 11
The indexkey can be iterated through directly and spares
search through a pencilkey. Strangely, the word 'PYTHON'
has the same sequence either way: [3, 5, 4, 0, 2, 1].
>> 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)
>
> This function has invalid syntax (the 'while' is indented for no
> apparent reason) which makes it even harder to figure out.
> Like the above function, it also uses a global variable to store
> its result.
The entire 'while' block inadvertently got an extra tab
in copying to email. That function was an embarrassing
kludge anyhow. Haven't eliminated storage in global
variables yet, and the telegraph function still doesn't
output in telegraphic standard (five letter groups), but...
#coltrans.py - Columnar Transposition Cipher
import string
keystring = raw_input('keyword please: ')
keylist = list(string.upper(keystring))
seqlist = []
idxlist = []
messagestring = raw_input('message please: ')
messagelist = 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 transpose(keyseq, message):
for number in range(len(keyseq)):
for letter in range(keyseq[number], len(message), len(keyseq)):
idxlist.append(message[letter])
def telegraph(result):
for number in range(len(result)):
print result[number],
def encipher():
unstray(keylist)
unstray(messagelist)
keysequence(keylist)
transpose(seqlist, messagelist)
telegraph(idxlist)
encipher()
> Now you should write the decoder!
Yeah, that too...