How to resolve the algorithm Playfair cipher step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Playfair cipher step by step in the J programming language
Table of Contents
Problem Statement
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet. The output of the encrypted and decrypted message must be in capitalized digraphs, separated by spaces.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Playfair cipher step by step in the J programming language
Source code in the j programming language
choose=: verb define
sel=. 'Q' e. y
alph=: (sel { 'JQ') -.~ a. {~ 65 + i.26
norm=: [: dedouble alph restrict ('I' I.@:=&'J'@]} ])`(-.&'Q')@.sel@toupper
''
)
restrict=: ] -. -.~
splitDigraph=: ,`([,'X',])@.((= {.) *. 2 | #@])
dedouble=: splitDigraph/&.|. NB. progressively split digraphs in string
choose 'Q'
setkey=: verb define
key=. ~.norm y,alph
ref=: ,/ 2{."1 ~."1 (,"0/~ alph) ,"1 norm 'XQV'
mode=. #. =/"2 inds=. 5 5#:key i. ref
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 {"_1 inds0 ,"2 3 inds1 ,:"2 inds2
i. 0 0
)
pairs=: verb define
2{."1 -.&' '"1 ~."1 (_2]\ norm y) ,"1 'XQV'
)
encrypt=: verb define
;:inv ;/ alt{~ref i. pairs y
)
decrypt=: verb define
, ref{~alt i. pairs y
)
choose 'IJ'
setkey 'playfair example'
encrypt 'Hide the gold in the tree stump'
BM OD ZB XD NA BE KU DM UI XM MO UV IF
decrypt 'BM OD ZB XD NA BE KU DM UI XM MO UV IF'
HIDETHEGOLDINTHETREXESTUMP
You may also check:How to resolve the algorithm Character codes step by step in the zkl programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Twelve statements step by step in the ERRE programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the REXX programming language