Playfair cipher: Difference between revisions

Content added Content deleted
Line 1,108:
function canonical(s)
s = replace(replace(uppercase(s), from => to), r"[^A-Z]" => "")
a, dupcount = [c for c in s], 0
b = deepcopy(a)
for i in 1:2:length(a)-1
if a[i] == a[i + 1]
a[isplice!(b, i+ 1] =+dupcount:i+dupcount, 'X')
dupcount += 1
end
end
s = String(ab)
return isodd(length(s)) ? s * "X" : s
end
Line 1,120 ⟶ 1,122:
# Translate key into an encoding 5x5 translation matrix.
keyletters = unique([c for c in canonical(key * "ABCDEFGHIJKLMNOPQRSTUVWXYZ")])
m = reshape(keyletters[1:25], 5, 5)
 
m = Char.((reshape(UInt8.(keyletters[1:25]), 5, 5)'))
# encod is a dictionary of letter pairs for encoding.
encod = Dict()
 
# Map pairs in same row of matrix m.
for i in 1:5, j in 1:5, k in 1:5
Line 1,170 ⟶ 1,171:
<pre>
Original: Hide the gold in...the TREESTUMP!!!
Encoded: MBBM OD BZZB DXXD ANNA EBBE UKKU MDDM IUUI XM ZKMO RZUV IYIF
Decoded: HI DE TH EG OL DI NT HE TR EX STES UMTU PXMP
</pre>