ADFGVX cipher: Difference between revisions
Content deleted Content added
m Minor code improvement. |
→{{header|Uiua}}: Changed the algorithm |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1,722:
candidates.emplace_back(word);
}
}
}
Line 2,734 ⟶ 2,733:
Encoded: DVVA FVX XXA FGF XVX GXA XXD GFA XXD
</pre>
=={{header|REXX}}==
<syntaxhighlight lang="rexx">
/* REXX */
cls
eol=x2c(0D0A) ; msg="ATTACKAT1200AM"
keyword= upper('lifeguard') ; cyph= 'ADFGVX'
s_sort= keyword ; new_key= ''
do while length(s_sort) > 0
nmax= 0
do i=1 to length(s_sort)
ch= substr(s_sort,i,1)
num= c2d(ch)
if num > nmax then do
nmax= num
max_i = i
end
end
s_sort= delstr(s_sort,max_i,1)
new_key= d2c(nmax)||new_key
end /* Alphabetical sorting */
j=0 ; num_str= '' ; rnd_s= ''
do while j < 36
num= random(0,35)
if wordpos(num,num_str) = 0 then do
j= j + 1
num_str= num_str||num||' '
if num >= 10 then do
num= num - 10 + x2d(41)
num= d2c(num)
end
rnd_s= rnd_s||num
end
end /* say 'Generated string: '||rnd_s||eol */
say 'Polybius square:'||eol
call tab cyph, rnd_s ,1
say "Only characters from the '"|| msg||"'"||eol
t= translate(rnd_s,' ',msg)
_t= translate(rnd_s,' ',t)
call tab cyph, _t ,1
len_c= length(cyph) ; cyph_T=''
do i=1 to len_c
ch_i= substr(cyph,i,1)
do j=1 to len_c
ch_j= substr(cyph,j,1)
cyph_T= cyph_T||ch_i||ch_j||' '
end
end
enc_msg= ''
do i=1 to length(msg)
ch= substr(msg,i,1)
j= pos(ch,rnd_s,1)
enc_msg= enc_msg||word(cyph_T,j)
end
say "Conversion by table: "||eol||eol||msg||" ==> "||enc_msg||eol
call tab keyword, enc_msg
len= length(keyword)
n_row= 0 ; column.= ''
do while enc_msg <> ''
parse var enc_msg 1 s1 +(len) enc_msg
n_row= n_row+1
do m= 1 to len
ch_m= substr(s1,m,1)
column.m= column.m||ch_m
end
end
s_lst= ''
do m= 1 to len
ch= substr(new_key,m,1)
i= pos(ch,keyword,1)
w_i= column.i
s_lst= s_lst||w_i||' '
end
row.= '' ; t_row= ''
do i=1 to len
w_i= word(s_lst,i)
do j=1 to n_row
row.j= row.j||substr(w_i,j,1)
end
end
do j=1 to n_row; t_row= t_row||row.j; end
say "Sorted by columns:"||eol
call tab new_key, t_row
say '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
say 'Encrypted message: '||s_lst
say '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
exit
tab:
parse arg h, s, p /* header, string, param */
lh= length(h) ;
s= h||copies('-',lh)||s ; ls= length(s)
h=' -'||h
t= '' ; j= 1
do i= 1 to ls by lh
row= substr(s,i,lh)
r_ch= ''
do l=1 to lh
ch= substr(row,l,1)
r_ch= r_ch||ch||' '
end
row= r_ch
if p <> '' then row= row||'|'||substr(h,j,1)
t= t||row||eol
j= j + 1
end
say t
return
</syntaxhighlight>
{{out}}
<pre>
Polybius square:
A D F G V X |
- - - - - - |-
D H 8 P U 4 |A
E T S C 5 9 |D
A 3 Z F R I |F
Y O 1 Q 7 W |G
J 2 G V N X |V
L 6 0 K M B |X
Only characters from the 'ATTACKAT1200AM'
A D F G V X |
- - - - - - |-
|A
T C |D
A |F
1 |G
2 |V
0 K M |X
Conversion by table:
ATTACKAT1200AM ==> FADDDDFADGXGFADDGFVDXFXFFAXV
L I F E G U A R D
- - - - - - - - -
F A D D D D F A D
G X G F A D D G F
V D X F X F F A X
V
Sorted by columns:
A D E F G I L R U
- - - - - - - - -
F D D D D A F A D
D F F G A X G G D
F X F X X D V A F
V
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Encrypted message: FDF DFX DFF DGX DAX AXD FGVV AGA DDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</pre>
Line 2,898 ⟶ 3,067:
Decoded: ATTACKAT1200AM</pre>
=={{header|Uiua}}==
Quick and simple solution for demonstration purposes.
<syntaxhighlight lang="uiua">
A ← ↯∞_2⊞⊟."ADFGVX"
P ← ⊏⍏[⍥⚂]⧻."ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
K ← "PACKHORSE"
Sw ← ⊜□≠" ".
Polybius ← setinv(/⊂⊡≡(⊚⌕):⊙:¤P A|⊏:P≡(⊢⊚⌕)⊙¤∩≡□↯∞_2:A)
SortByK ← setinv(⊏⍏⟜(⍉⬚" "↯⊟∞⧻)K|▽≠" "./⊂⍉⊏⍏⍏K)
Str ← setinv(/$"_ _"Sw/⊂≡(⊂:" ")|≡(⊢°□◇⬚" "↯⊟∞)/↥≡◇⧻.Sw)
Pt ← "ATTACKAT1200AM"
&p $"Key = _\nPolybius:\n_\nPlaintext = _"K↯⊟.√⧻.P Pt
&p$"Crypttext = _". Str SortByK Polybius Pt
&p$"Plaintext = _" °Polybius °SortByK °Str
</syntaxhighlight>
{{out}}
<pre>
Key = PACKHORSE
Polybius:
╭─
╷ "7ENLUB"
"WOJ69G"
"8ZFAKP"
"I4R3C1"
"V5TMHD"
"SY0XQ2"
╯
Plaintext = ATTACKAT1200AM
Crypttext = GFX VVX GXV VGX FFF FVF FVXG FFF GGG
Plaintext = ATTACKAT1200AM
</pre>
=={{header|Wren}}==
Line 2,903 ⟶ 3,105:
{{libheader|Wren-seq}}
{{libheader|Wren-str}}
<syntaxhighlight lang="
import "./ioutil" for FileUtil
import "./seq" for Lst
import "./str" for Char, Str
var rand = Random.new()
Line 3,029 ⟶ 3,231:
Decrypted : ATTACKAT1200AM
</pre>
|