ADFGVX cipher: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 28:
{{trans|Nim}}
 
<langsyntaxhighlight lang=11l>V adfgvx = ‘ADFGVX’
 
F encrypt(plainText, polybius, key)
Line 94:
 
V plainText = decrypt(cipherText, polybius, key)
print("\nDecrypted : "plainText)</langsyntaxhighlight>
 
{{out}}
Line 119:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
// ADFGVX cipher. Nigel Galloway: August 23rd., 2021
let polybus=let n=[|yield! {'A'..'Z'}; yield! {'0'..'9'}|] in MathNet.Numerics.Combinatorics.GeneratePermutation 36|>Array.map(fun g->n.[g]),[|'A';'D';'F';'G';'V';'X'|]
Line 137:
let encrypt,decrypt=ADFGVX polybus "nigel" //Using "nigel" as the key no hacker will guess that!
let n=encrypt "ATTACKAT1200AM" in printfn $"\nATTACKAT1200AM encrypted is %s{n} which decrypted is %s{decrypt n}"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 154:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang=go>package main
 
import (
Line 336:
plainText2 := decrypt(polybius, key, cipherText)
fmt.Println("\nDecrypted :", plainText2)
}</langsyntaxhighlight>
 
{{out}}
Line 362:
 
=={{header|J}}==
Implementation:<langsyntaxhighlight lang=J>polybius=: {{6 6$8 u:({~?~&#)(48+i.10),65+i.26}}
lenword=: {{ ;({~ ?@#)(#~ (-:~.)@>)(#~ y=#@>)cutLF fread'unixdict.txt'}}
ADFGVX=: {{ deb,' ',.n/:~|:(-#n)]\'ADFGVX'{~,($m)#:(,m)i.y([-.-.),m }}
XVGFDA=: {{ (,m){~($m)#.'ADFGVX'i._2]\deb,|:(>cut y)/:/:n }}</langsyntaxhighlight>
 
Example:<langsyntaxhighlight lang=J> echo W=: lenword 9
roughcast
echo P=: polybius ''
Line 379:
FFF FGF FFF GXD XDG FDGG XDX XXA GAD
echo D=: P XVGFDA W E
ATTACKAT1200AM</langsyntaxhighlight>
 
That said, note that we could also eliminate spaces from the encrypted text, as they are recoverable if we have the key word.
 
<langsyntaxhighlight lang=J>spaces=: {{deb y#inv~,0,.(/:n){*|:_9]\>:i.#y=.y-.' '}}
 
echo S=: E-.' '
FFFFGFFFFGXDXDGFDGGXDXXXAGAD
P spaces W S
FFF FGF FFF GXD XDG FDGG XDX XXA GAD</langsyntaxhighlight>
 
(Technically, we do not need the Polybius square to recover the spaces, but it's passed as an argument here for symmetry.)
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>"""
The ADFGVX cipher.
See also eg. https://www.nku.edu/~christensen/092hnr304%20ADFGVX.pdf
Line 464:
println("Encoded: $encoded")
println("Decoded: $decoded")
</langsyntaxhighlight>{{out}}
<pre>
Polybius: L4VZJIB8OXGFM1H3CTNKU9PE75WQ2DAYRS06, Key: SUNFLOWER
Line 476:
It started as a translation, but actually we use a different method, better suited to Nim, to encrypt and decrypt. And there are many other differences. Output is similar though.
 
<langsyntaxhighlight lang=Nim>import algorithm, random, sequtils, strutils, sugar, tables
 
const Adfgvx = "ADFGVX"
Line 576:
 
let plainText = cipherText.decrypt(polybius, key)
echo "\nDecrypted : ", plainText</langsyntaxhighlight>
 
{{out}}
Line 599:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/ADFGVX_cipher
Line 649:
}
 
sub transpose { map join('', map {s/.// ? $& : ''} @_), 1 .. length $_[0] }</langsyntaxhighlight>
{{out}}
<pre>
Line 674:
=={{header|Phix}}==
{{trans|Wren}} We can make some nice use of the standard builtin routines here, with only a modest amount of whitespace cleanup.
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">ADFGVX</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ADFGVX"</span><span style="color: #0000FF;">,</span>
Line 747:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nPlainText : %s\n\nEncrypted : %s\n\nDecrypted : %s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">plaintext</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">encrypted</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">decrypted</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
Output matches Wren/Nim/wp when the appropriate lines are uncommented.
 
Line 755:
According to historical sources the original encrypted text was spaced in 5 character blocks regardless
of message and key length, which means that decryption should not rely on spacing.
<langsyntaxhighlight lang=python>"""
The ADFGVX cipher implemented as a Python class
See also eg. https://www.nku.edu/~christensen/092hnr304%20ADFGVX.pdf
Line 821:
print('Encoded: ', ENCODED)
print('Decoded: ', DECODED)
</langsyntaxhighlight>{{out}}
<pre>
Polybius: A9GKMF1DQRSBVX8Z0WTEJLOPY5U4CN2H76I3, key: volcanism
Line 831:
=={{header|Raku}}==
Slightly different results from every other entry so far. See discussion page for reasons. It is impossible to tell from casual observation which column comes first in the Raku example. In every other (so far), the sub group with 4 characters is the first column.
<langsyntaxhighlight lang=perl6>srand 123456; # For repeatability
 
my @header = < A D F G V X >;
Line 863:
my @order = $key.comb.pairs.sort( *.value )».key.pairs.sort( *.value )».key;
%cypher{ ( grep { /\w/ }, flat [Z] @order.map( { |@text.batch($key-length)».[$_] } ) ).batch(2)».join }.join;
}</langsyntaxhighlight>
{{out}}
<pre style="line-height:1.2em;">Key: GHOSTLIKE
Line 894:
{{libheader|Wren-seq}}
{{libheader|Wren-str}}
<langsyntaxhighlight lang=ecmascript>import "random" for Random
import "/ioutil" for FileUtil
import "/seq" for Lst
Line 997:
System.print("\nEncrypted : %(cipherText)")
var plainText2 = decrypt.call(polybius, key, cipherText)
System.print("\nDecrypted : %(plainText2)")</langsyntaxhighlight>
 
{{out}}
Line 1,023:
 
=={{header|REXX}}==
<langsyntaxhighlight lang=rexx>
/* REXX */
cls
Line 1,142:
say t
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
10,333

edits