I have coded Rail Fence Cipher in Python. I was wondering if there could be a better solution.
For those who don't know what rail fence cipher is, it is basically a method of writing plain text in a way it creates linear pattern in a spiral way. Example - when "FOOBARBAZ" rail-fenced using key of 3.
F . . . A . . . Z . . .
. O . B . R . A . Q . X
. . O . . . B . . . U .
Reading the above spiral line-by-line, the cipher text becomes "FAZOBRAQXOBU". Read more at - Rail fence - Wikipedia.
def cipher(s, key, graph=False) :
down=True
raw_out=[]
out=''
i=0
for x in range(key) :
raw_out.append({})
for pos in range(len(s)) :
raw_out[i][pos]=s[pos]
if i==key-1 :
down=False
if i==0 :
down=True
if down :
i=i+1
else :
i=i-1
for p in raw_out :
for q in p :
out+=p[q]
if graph :
return raw_out
return out
def decipher(s, key) :
map_list=cipher(s, key, True) #CREATING JUST FOR MAPPING - WHICHth CHARACTER OF THE STRING - IS WHICHth CHARACTER OF THE CIPHER
new={}
out=''
s_counter=0
for x in map_list :
for y in x :
new[y]=s[s_counter]
s_counter+=1
for p in new :
out+=new[p]
return map_list
I was wondering if there was any better way of doing this, since my procedure is very costly, it a uses couple of dictionaries.
Code in any language is welcomed.