I have created a script to encrypt and decrypt a text message based on the bifid cipher(wiki about bifid)
everything works fine IF the text message does only contain letters.
How can I integrate the ability off spaces etc... the best way?
by adding them to my alphabet might solve it?
It will be very difficult to understand the code because I didn't add help messages, but I just follow the way like it is described on the wikipedia page.
(the pf is the passphrase which is passed by globals)
def bifid(txt):
global crypt
l1=[]
l2=[]
l3=[]
l4=[]
l5=[]
l0=[]
i=0
temp=list(pf)
while i < len(temp):
if temp[0] in l1 or temp[0] in l2 or temp[0] in l3 or temp[0] in l4 or temp[0] in l5:
pass
else:
if len(l1)<5: l1.append(temp[0])
elif len(l2)<5: l2.append(temp[0])
elif len(l3)<5: l3.append(temp[0])
elif len(l4)<5: l4.append(temp[0])
elif len(l5)<5: l5.append(temp[0])
temp.pop(0)
alph=list(ascii_lowercase)
while i < len(alph):
if alph[0] in l1 or alph[0] in l2 or alph[0] in l3 or alph[0] in l4 or alph[0] in l5:
pass
else:
if len(l1)<5: l1.append(alph[0])
elif len(l2)<5: l2.append(alph[0])
elif len(l3)<5: l3.append(alph[0])
elif len(l4)<5: l4.append(alph[0])
elif len(l5)<5: l5.append(alph[0])
alph.pop(0)
if crypt=="en":
l6=list(txt)
l7=[]
l8=[]
for i in l6:
if i in l1: l7.append('1');l8.append(str(l1.index(i)+1))
elif i in l2: l7.append('2');l8.append(str(l2.index(i)+1))
elif i in l3: l7.append('3');l8.append(str(l3.index(i)+1))
elif i in l4: l7.append('4');l8.append(str(l4.index(i)+1))
else: l7.append('5');l8.append(str(l5.index(i)+1))
l9="".join(l7)+"".join(l8)
l9=[l9[i:i+2] for i in range(0,len(l9),2)]
l10=[]
for i in l9:
r=list(i)
if r[0]=="1": l10.append(l1[int(r[1])-1])
elif r[0]=="2": l10.append(l2[int(r[1])-1])
elif r[0]=="3": l10.append(l3[int(r[1])-1])
elif r[0]=="4": l10.append(l4[int(r[1])-1])
else: l10.append(l5[int(r[1])-1])
txt="".join(l10)
else:
l6=list(txt)
l7=[]
for i in l6:
if i in l1: l7.append('1'+str(l1.index(i)+1))
elif i in l2: l7.append('2'+str(l2.index(i)+1))
elif i in l3: l7.append('3'+str(l3.index(i)+1))
elif i in l4: l7.append('4'+str(l4.index(i)+1))
else: l7.append('5'+str(l5.index(i)+1))
l7="".join(l7)
i=len(l7)/2
o,p=list(l7[0:i]),list(l7[i::])
l8=[]
i=0
for item in o:
if o[i]=="1": l8.append(l1[int(p[i])-1])
elif o[i]=="2": l8.append(l2[int(p[i])-1])
elif o[i]=="3": l8.append(l3[int(p[i])-1])
elif o[i]=="4": l8.append(l4[int(p[i])-1])
else: l8.append(l5[int(p[i])-1])
i+=1
txt="".join(l8)
return txt