Playfair cipher: Difference between revisions
m
→{{header|Phix}}
(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)
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
</pre>
|