Playfair cipher: Difference between revisions

m
(Phix)
Line 1,733:
 
=={{header|Phix}}==
Originally translated from Kotlin, now uses a combined routine (playfair) for encoding and decoding, and direct char lookups, and x removal.
<lang Phix>sequence table,
findchar
Line 1,794:
end function
function remove_x(string text)
for i=2 to length(text)-1 do
if text[i]='X'
and ((text[i-1]=' ' and text[i-2]=text[i+1]) or
(text[i+1]=' ' and text[i-1]=text[i+2])) then
text[i] = '_'
end if
end for
return text
end function
 
function playfair(string text, integer step, sequence d)
string res = ""
Line 1,815 ⟶ 1,826:
function encode(string plaintext)
plaintext =return playfair(clean_text(plaintext),2,p1)
return playfair(plaintext,2,p1)
end function
 
function decode(string ciphertext)
-- ciphertext includes spaces we need to skip, hence by 3
return remove_x(playfair(ciphertext,3,m1))
end function
Line 1,850 ⟶ 1,860:
 
Encoded text is : BM ND ZB XD KY BE JV DM UI XM MN UV IF
Decoded text is : HI DE TH EG OL DI NT HE TR EXE_ ES TU MP
</pre>
 
7,820

edits