Bifid cipher: Difference between revisions
m
syntax highlighting fixup automation
(J: move normalization out of the encrypt/decrypt routines and show an example with a 9x9 reference "square") |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 62:
<br><br>
=={{header|J}}==
Implementation:<
normalize=: {{ rplc&'JI'(toupper y)([-.-.)alpha }}
bifid=: {{ m{~_2 (t&#.)\,|:(t,t=.%:#m)#:m i.y([-.-.)m }}
difib=: {{ m{~t#.|:(|.@$$,)(t,t=.%:#m)#:m i.y([-.-.)m }}</
This is pretty much a literal implementation of the algorithm, except that our indices range from 0..4 instead of 1..5 (or, for letter indices, they range from 0..24 instead of 1..25).
Line 71:
Much of the implementation is about converting between a two digit base 5 representation and a single digit numeric representation. The rest is simple array manipulations to make the rest come out right.
Task examples:<
ref2=: 'BGWKZQPNDSIOAXEFCLUMTHYVR'
ref3=: 'PLAYFIREXMBCDGHKNOQSTUVWZ'
Line 94:
TgqhpqpqxzpxfqzoKqhxw/O3eWH53BYw`+Be8F1
(_81{.123{.a.)difib(_81{.123{.a.)bifid 'The invasion will start on the first of January'
TheinvasionwillstartonthefirstofJanuary</
=={{header|Julia}}==
Using the Raku example's test messages.
<
function encrypt(message, poly)
Line 123:
println("\n Message: $text\n Encrypted: $encrypted\n Decrypted: $decrypted\n\n")
end
</
<pre>
Using polybius:
Line 169:
=={{header|Perl}}==
<
use builtin <indexed floor>;
use experimental qw(builtin for_list);
Line 215:
say 'Encrypted : ' . (my $encrypted = encrypt $message, %Ptable);
say 'Decrypted : ' . decrypt $encrypted, %Ptable;
}</
{{out}}
<pre>Using polybius:
Line 254:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">encrypt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">decrypt</span>
Line 294:
<span style="color: #008000;">"The invasion will start on the first of January 2023."</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">messages</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t</span><span style="color: #0000FF;">],</span><span style="color: #000000;">polybii</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 332:
=={{header|Python}}==
<
import math
import pprint
Line 438:
if __name__ == "__main__":
main()
</syntaxhighlight>
{{out}}
Line 492:
Technically incorrect as the third part doesn't "Convert ... to upper case and ignore spaces".
<syntaxhighlight lang=raku
my $n = $text.chars.sqrt.narrow;
$text.comb.kv.map: { $^v => ($^k % $n, $k div $n).join: ' ' }
Line 515:
say "Encrypted : " ~ my $encrypted = encrypt $message, %polybius;
say "Decrypted : " ~ decrypt $encrypted, %polybius;
}</
{{out}}
<pre>Using polybius:
Line 557:
However, the following just uses the standard version of the cipher.
<
import "./seq" for Lst
Line 614:
System.print("Decrypted : %(decrypted)")
if (i < msgs.count-1) System.print()
}</
{{out}}
|