Bifid cipher: Difference between revisions

julia example
(julia example)
Line 89:
 
We could include both I and J by increasing the square size to 64: take all ascii characters in the range from 32 (space) to 126 (tilde). Discard the lower case letters. Discard four other characters. Use the remainder for the square.
 
=={{header|Julia}}==
Using the Raku example's test messages.
<lang ruby>polybius(text) = Char.(reshape(Int.(collect(text)), isqrt(length(text)), :)')
 
function encrypt(message, poly)
positions = [findall(==(c), poly)[1] for c in message]
numbers = vcat([c[1] for c in positions], [c[2] for c in positions])
return String([poly[numbers[i], numbers[i+1]] for i in 1:2:length(numbers)-1])
end
 
function decrypt(message, poly)
n = length(message)
positions = [findall(==(c), poly)[1] for c in message]
numbers = reduce(vcat, [[c[1], c[2]] for c in positions])
return String([poly[numbers[i], numbers[i+n]] for i in 1:n])
end
 
 
for (key, text) in [("ABCDEFGHIKLMNOPQRSTUVWXYZ", "ATTACKATDAWN"), ("BGWKZQPNDSIOAXEFCLUMTHYVR", "FLEEATONCE"),
([' '; '.'; 'A':'Z'; 'a':'z'; '0':'9'], "The invasion will start on the first of January 2023.")]
poly = polybius(key)
encrypted = encrypt(text, poly)
decrypted = decrypt(encrypted, poly)
println("Using polybius:")
display(poly)
println("\n Message: $text\n Encrypted: $encrypted\n Decrypted: $decrypted\n\n")
end
</lang>{{out}}
<pre>
Using polybius:
5×5 Matrix{Char}:
'A' 'B' 'C' 'D' 'E'
'F' 'G' 'H' 'I' 'K'
'L' 'M' 'N' 'O' 'P'
'Q' 'R' 'S' 'T' 'U'
'V' 'W' 'X' 'Y' 'Z'
 
Message: ATTACKATDAWN
Encrypted: DQBDAXDQPDQH
Decrypted: ATTACKATDAWN
 
 
Using polybius:
5×5 Matrix{Char}:
'B' 'G' 'W' 'K' 'Z'
'Q' 'P' 'N' 'D' 'S'
'I' 'O' 'A' 'X' 'E'
'F' 'C' 'L' 'U' 'M'
'T' 'H' 'Y' 'V' 'R'
 
Message: FLEEATONCE
Encrypted: UAEOLWRINS
Decrypted: FLEEATONCE
 
 
Using polybius:
8×8 Matrix{Char}:
' ' '.' 'A' 'B' 'C' 'D' 'E' 'F'
'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N'
'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V'
'W' 'X' 'Y' 'Z' 'a' 'b' 'c' 'd'
'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l'
'm' 'n' 'o' 'p' 'q' 'r' 's' 't'
'u' 'v' 'w' 'x' 'y' 'z' '0' '1'
'2' '3' '4' '5' '6' '7' '8' '9'
 
Message: The invasion will start on the first of January 2023.
Encrypted: SejxqrEierbmrDiCjrDeJsbu89DWCHkgGS9E6tAG5 Ks2PBfCq uH
Decrypted: The invasion will start on the first of January 2023.
 
</pre>
 
=={{header|Raku}}==
4,105

edits