alphabet1:="abcdefghiklmnopqrstuvwxyz"; makeplayfairsquare:=function(key) local square,square1,list,i,a,b,pos; list:=List([1..Length(alphabet1)],x->false); square:=[]; pos:=[]; for i in [1..Length(key)] do if list[Position(alphabet1,key[i])] = false then Add(square,key[i]); list[Position(alphabet1,key[i])]:=true; fi; od; Print(square,"\n"); for i in [1..Length(alphabet1)] do if list[Position(alphabet1,alphabet1[i])] = false then Add(square,alphabet1[i]); fi; od; square1:=List([1..5],x->[]); for i in [1..Length(alphabet1)] do a:= ((i-1) mod 5) +1; b:= (i-a)/5; square1[b+1][a]:=square[i]; pos[Position(alphabet1,square[i])]:=[b+1,a]; od; return [square1,pos]; end; playfair:=function(string,square) local newstring,table, cipher,lookup,i,firstrow,firstcol,secondrow,secondcol; # # make new string # # note we also have to care of triples example : less seven # newstring:=[]; for i in [1..Length(string)-1] do # # get rid off j's; replace by i's; # if string[i] = 'j' then string[i]:='i'; fi; Add(newstring,string[i]); if string[i+1] = string[i] and ((Length(newstring) mod 2) = 1 )then Add(newstring,'x'); fi; od; Add(newstring,string[Length(string)]); if (Length(newstring) mod 2) = 1 then Add(newstring,'x'); fi; Print(newstring,"\n"); # # now make cipher # cipher:=[]; lookup:=square[2]; table:=square[1]; i:=1; while (i <=Length(newstring)-1) do firstrow:=lookup[Position(alphabet1,newstring[i])][1]; firstcol:=lookup[Position(alphabet1,newstring[i])][2]; secondrow:=lookup[Position(alphabet1,newstring[i+1])][1]; secondcol:=lookup[Position(alphabet1,newstring[i+1])][2]; if firstrow = secondrow then Add(cipher,table[firstrow][(firstcol mod 5) +1]); Add(cipher,table[firstrow][(secondcol mod 5) +1]); fi; if firstcol = secondcol then Add(cipher,table[(firstrow mod 5) + 1][firstcol]); Add(cipher,table[(secondrow mod 5) + 1][firstcol]); fi; if firstrow <> secondrow and firstcol <> secondcol then Add(cipher,table[firstrow][secondcol]); Add(cipher,table[secondrow][firstcol]); fi; i:=i+2; od; return cipher; end;