physics, maths & computer science

Constructing the ‘Viginere square’, and using it to encipher and decipher requires plenty of iteration.

import itertools as itertools
from itertools import izip
from itertools import cycle

enter plaintext and the cipher key:

cipher_key = str(raw_input("enter the cipher key (word): "))
plaintext = str(raw_input("enter plaintext: "))

create the vigniere square, to encipher and decipher
square = {(num+1):(num+1) for num in range(0,26,1)}
alphalist = [chr(num+97) for num in range(0,26,1)]
for k,v in square.iteritems():
linelista = alphalist[v:]
linelistb = alphalist[:v]
linelistc = []
for letter in linelista:
linelistc.append(letter)
for letter in linelistb:
linelistc.append(letter)
square[k] = linelistc
linelista = []
linelistb = []
linelistc = []
for k,v in square.iteritems():
new_key = square[k][0]
square[new_key] = square.pop(k)

import the key and prepare the plaintext

key_list = []
for letter in cipher_key:
key_list.append(letter)
plaintext = plaintext.lower()
plaintext = plaintext.replace(" ", "")
plaintext = list(plaintext)
plaintext_list = []
for letter in plaintext:
plaintext_list.append(letter)

estbalish the cipher alphabets for each letter in the plaintext, using the key
alphalist_match = []
for item in itertools.izip(plaintext_list, itertools.cycle(key_list)):
alphalist_match.append(item)

choose the correct cipher letter for each plaintext letter
alphalist_index = zip(alphalist,range(0,25,1))
alphalist_index = dict(alphalist_index)
ciphertext = []
for item in alphalist_match:
a,b = item
letter_num = alphalist_index[a]
cipher_letter = square[b][letter_num]
ciphertext.append(cipher_letter)
ciphertext = ''.join(ciphertext).upper()
print "ciphertext is: ", ciphertext

Gives:
ciphertext is: ZPDXVPAZHSLZBHIWZBKMZNM

Decipher, using the square above, and key provided.

cipher_key = str(raw_input("enter the cipher key (word): "))
ciphertext = str(raw_input("enter ciphertext: "))

import the key and prepare the ciphertext

key_list = []
for letter in cipher_key:
key_list.append(letter)
ciphertext = ciphertext.lower()
ciphertext = ciphertext.replace(" ", "")
ciphertext = list(ciphertext)
ciphertext_list = []
for letter in ciphertext:
ciphertext_list.append(letter)

estbalish the cipher alphabets for each letter in the ciphertext, using the key

alphalist_match = []
for item in itertools.izip(ciphertext_list, itertools.cycle(key_list)):
alphalist_match.append(item)

choose the correct plaintext letter for each ciphertext letter
alphalist_return_index = zip(alphalist,range(0,25,1))
alphalist_return_index = dict(alphalist_return_index)
for k,v in alphalist_return_index.iteritems():
new_key = v
alphalist_return_index[new_key] = alphalist_return_index.pop(k)
alphalist_return_index[new_key] = k

perform the substitution
plaintext = ''
for item in alphalist_match:
a,b = item
for index,letter in enumerate(square[b]):
if letter==a:
return_index = index
plaintext_letter = alphalist_return_index[return_index]
plaintext = plaintext+plaintext_letter
print "plaintext is: ",plaintext

gives:
plaintext is: diverttroopstoeastridge