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

Nevermind I found the solution myself, I just delete all non-ascii characters before I do the crypting and store the location + the char in a list.
After the encryption I restore them ;)

Can someone tell me why the following code does not work if there are 2 spaces after each other(in txt)?, one off the two spaces stays in l6 although it should be stored in nonascii and deleted form l6 :confused:

c=0
d=0
nonascii=[]
l6=list(txt)
for char in l6:
    print char + "dd"
    if char in ascii_lowercase:
        pass
    else:
        nonascii.append([d,char])
        l6.pop(c)
        d+=1
    c+=1
    d+=1

Edited 6 Years Ago by Kruptein: wrong intendation

I don't see any reason why a space and number characters couldn't be added to your Polybius square.

I know it is possible, but just in this case why won't it work?

*It is possible with a 6x6 like the adfgvx code, but then I still have the problems with other chars like "!","&",...
so after all I still need this...

Edited 6 Years Ago by Kruptein: n/a

characters = 'abcdefghijklmnopqrstuvwxyz' 
characters += '0123456789'
characters += ' ,.?!()#;:+-='

print len(characters)  # 49  -->  7 x 7 Polybius square

Okay your right,
thanks, but beside do you know why it didn't work with 2 spaces after each other

that's why I used the d+=1,
but you can forget about that, with your tip about using a larger polibius square, I was able to solve my problem!

(I used a 8x8 square though)

Edited 6 Years Ago by Kruptein: n/a

This question has already been answered. Start a new discussion instead.