Playfair cipher: Difference between revisions
Content added Content deleted
(Added zkl) |
(J) |
||
Line 220: | Line 220: | ||
Encoded: BM OD ZB XD NA BE KU DM UI XM MO UV IF |
Encoded: BM OD ZB XD NA BE KU DM UI XM MO UV IF |
||
Decoded: HI DE TH EG OL DI NT HE TR EX ES TU MP</pre> |
Decoded: HI DE TH EG OL DI NT HE TR EX ES TU MP</pre> |
||
=={{header|J}}== |
|||
Implementation: |
|||
<lang J>choose=:3 :0 |
|||
sel=. 'Q'e.y |
|||
alph=:(sel{'JQ')-.~a.{~65+i.26 |
|||
norm=: alph intersect ('I' I.@:=&'J'@]} ])`(-.&'Q')@.sel@toupper |
|||
'' |
|||
) |
|||
intersect=: ] -. -.~ |
|||
choose 'Q' |
|||
setkey=:3 :0 |
|||
key=: ~.norm y,alph |
|||
pairs=: 2{."1 ~."1 (,"0/~alph),"1 norm 'XQV' |
|||
mode=: #.=/"2 inds=:5 5#:key i.pairs |
|||
inds0=: (0 3,:2 1)&{@,"2 inds |
|||
inds1=:5|1 0+"1 inds NB. same column |
|||
inds2=:5|0 1+"1 inds NB. same row |
|||
alt=:,/key{~5 #.mode{"_2 inds0,"2 3 inds1,:"2 inds2 |
|||
ref=:,/pairs |
|||
i.0 0 |
|||
) |
|||
encrypt=:3 :0 |
|||
,alt{~ref i. 2{."1 -.&' '"1 ~."1 (_2]\ norm y),"1 'XQV' |
|||
) |
|||
decrypt=:3 :0 |
|||
,ref{~alt i. 2{."1 -.&' '"1 ~."1 (_2]\ norm y),"1 'XQV' |
|||
)</lang> |
|||
Example use: |
|||
<lang J> choose 'IJ' |
|||
setkey 'playfair example' |
|||
encrypt 'Hide the gold in the tree stump' |
|||
BMODZBXDNABEKUDMUIXMKZZRYI |
|||
decrypt 'BMODZBXDNABEKUDMUIXMKZZRYI' |
|||
HIDETHEGOLDINTHETREXSTUMPX</lang> |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |