Jump to content

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")
m (syntax highlighting fixup automation)
Line 62:
<br><br>
=={{header|J}}==
Implementation:<langsyntaxhighlight lang=J>alpha=: a.{~65+i.26
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 }}</langsyntaxhighlight>
 
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:<langsyntaxhighlight lang=J>ref1=: ~.normalize alpha
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</langsyntaxhighlight>
 
=={{header|Julia}}==
Using the Raku example's test messages.
<langsyntaxhighlight lang=julia>polybius(text) = Char.(reshape(Int.(collect(text)), isqrt(length(text)), :)')
 
function encrypt(message, poly)
Line 123:
println("\n Message: $text\n Encrypted: $encrypted\n Decrypted: $decrypted\n\n")
end
</langsyntaxhighlight>{{out}}
<pre>
Using polybius:
Line 169:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>use v5.36;
use builtin <indexed floor>;
use experimental qw(builtin for_list);
Line 215:
say 'Encrypted : ' . (my $encrypted = encrypt $message, %Ptable);
say 'Decrypted : ' . decrypt $encrypted, %Ptable;
}</langsyntaxhighlight>
{{out}}
<pre>Using polybius:
Line 254:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 332:
 
=={{header|Python}}==
<langsyntaxhighlight lang=python>"""Bifid cipher. Requires Python >=3.7."""
import math
import pprint
Line 438:
if __name__ == "__main__":
main()
</syntaxhighlight>
</lang>
 
{{out}}
Line 492:
Technically incorrect as the third part doesn't "Convert ... to upper case and ignore spaces".
 
<syntaxhighlight lang=raku perl6line>sub polybius ($text) {
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;
}</langsyntaxhighlight>
{{out}}
<pre>Using polybius:
Line 557:
 
However, the following just uses the standard version of the cipher.
<langsyntaxhighlight lang=ecmascript>import "./str" for Str
import "./seq" for Lst
 
Line 614:
System.print("Decrypted : %(decrypted)")
if (i < msgs.count-1) System.print()
}</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.