physics, maths & computer science

There are two levels of transposition –  2 and 3.

to encrypt, enter plaintext, remove spaces and capitals (at least),

plaintext = raw_input("enter plaintext string: ")
cs = int(raw_input("enter strength: 2 or 3: "))

plaintext = plaintext.lower()
plaintext = plaintext.replace(" ", "")

encipher by number of ‘rails’, 2 or 3
i=0
ct = ""
while i < cs:
t = str(i)
index = i
t = t + "a"
t = plaintext[index::cs]
ct = ct+t
i = i +1
ct = ct.upper()
print "ciphertext is: ",ct

gives:
enter plaintext string: Hello World
enter strength: 2 or 3: 3
ciphertext is: HLODEORLWL

to decipher, reverse the process, generally speaking, however the length of string is problem for reformulation, need to pad out for even groups of 2 or 3:

ct = raw_input("enter ciphertext: ")
cs = int(raw_input("enter cryptographic strength (2 or 3): "))

k = cs
length = len(ct)
if cs == 3:
if length%3 == 2:
length = length+1
ct = ct+'*'
elif length%3 ==1:
ct = ct+'*'
splice = round((len(ct)/3))
splice = int(splice)+1
first = ct[:splice]
second = ct[splice:-splice]+'*'
third = ct[-splice:]
ct = first+second+third
length = length+2
elif cs == 2:
while length%2 !=0:
length = length+1
ct = ct+'*'

prepare a list with either two or three groups of letters
stride = length/k
j = 0
pt_prep = ""
ptl_prep = ['*' for x in range(k)]
k = k-1
while j < length:
index = j
t = ct[index:(index+stride)]
ptl_prep[k] = t
pt_prep = t+pt_prep
k = k - 1
j = j + stride

reformulate the plaintext drawing one letter of each group in sequence and adding to the plaintext list.

i = 0
x = 0
k = cs
loops = len(ct)/k
pt = ['a' for y in range(len(pt_prep))]
if k == 3:
while i < loops: if i > len(ptl_prep[2]):
break
elif i< len(ptl_prep[2]): a = ptl_prep[2][i] pt[x]= a x = x ++ 1 if i > len(ptl_prep[1]):
break
elif i < len(ptl_prep[1]): b = ptl_prep[1][i] pt[x] = b x = x ++ 1 if i > len(ptl_prep[2]):
continue
elif i== len(ptl_prep[2]):
x = x ++ 1
else:
c = ptl_prep[0][i]
pt[x] = c
x = x ++ 1
i = i ++ 1
else:
while i < loops:
a = ptl_prep[1][i]
pt[x]= a
x = x ++ 1
b = ptl_prep[0][i]
pt[x] = b
x = x ++ 1
i = i ++ 1

filter out padding (*), and return plaintext

pt = filter(lambda ch: ch not in '*', pt)
pt = ''.join(pt)
pt = pt.lower()
print "plaintext is: ",pt

gives:

enter ciphertext: HLODEORLWL
enter cryptographic strength (2 or 3): 3
[‘H’, ‘E’, ‘L’, ‘L’, ‘O’, ‘W’, ‘O’, ‘R’, ‘L’, ‘D’]
plaintext is: helloworld