Playfair cipher: Difference between revisions

→‎{{header|REXX}}: added more diagnostic output, added "possible text" (which deletes the character used for double characters in the original text), used preferred spelling of cipher. -- ~~~~
(→‎{{header|REXX}}: fixed two unset variables. -- ~~~~)
(→‎{{header|REXX}}: added more diagnostic output, added "possible text" (which deletes the character used for double characters in the original text), used preferred spelling of cipher. -- ~~~~)
Line 361:
=={{header|REXX}}==
Quite a bit of the REXX code deals with error checking, accepting arguments, and displaying the options used.
<lang rexx>/*REXX program implements a PLAYFAIR cyphercipher (encryption & decryption).*/
parse arg key . 1 oldK noXomit _ . '(' oldXtext /*TEXT is the phrase to be used*/
if oldKkey =='' | oldKkey ==',' then oldKdo; key='Playfair example.'; oldK=key " ◄───the default."; end
if noXomit=='' | noXomit==',' then noXomit='J' /*the "omitted" character. */
if oldX text='' then oldXtext='Hide the gold in the tree stump!!' /*default.*/
key =scrub(key) /*use only Latin alphabet letters*/
say ' old cypher=' oldK
oldKnewText=scrub(oldKtext) /* " " " " /*use only" Latin alphabet letters*/
newX=scrub(oldX) /* " " " " " */
if _\=='' then call err 'too many arguments specified.'
if newXnewText=='' then call err 'PHRASETEXT is empty or has no letters'
if length(noXomit)\==1 then call err '"omitted"OMIT letter must be only one letter'
if \datatype(noXomit,'M') then call err '"omitted"OMIT letter must be a Latin alphabet letter.'
if pos(noXomit,oldXtext)\==0 then call err 'PHRASETEXT can''t contain the "OMIT" character: ' noXomit
newK= upper omit /*uppercase [↓]the remove anyOMIT duplicate charscharacter.*/
fill=space(translate('ABCDEFGHIJKLMNOPQRSTUVWXYZ',,noXomit),0) /*removeelide NOXomit*/
newK= /* [↓] remove any duplicate chars*/
newKey= do j=1 for length(oldK); _=substr(oldK,j,1) /* [↓] remove any duplicate chars*/
ifdo posj=1 for length(_,newKkey)==0; then newK=newK || _=substr(key,j,1)
if pos(_,newKey)==0 then newKey=newKey || _
end /*j*/
xx='X'; if omit==xx then xx='Q' /*char used for double characters*/
if length(newKnewKey)<3 then call err 'cyphercipher key is too short, must be > 2 characterunique characters.'
fill=space(translate(fill,,newK),0) /*remove any cypher characters. */
gridfill=leftspace(translate(newK || fill,26,newKey),0) /*useremove onlyany thecipher first 25 charscharacters. */
grid=left(newKey || fill,26) /*use only the first 25 chars. */
say ' new cypher=' newK
say ' old phrase=cipher: ' oldXstrip(oldK)
say ' new phrase=cipher: ' newXnewKey
say ' newomit char : digram=' digram(newX)omit
say 'double char : ' xx
say ' old cypher=text : ' oldKstrip(text)
say ' new cypher=text : ' newKnewText
say 'digram text ↔: ' digram(newText)
#=0
do row =1 for 5 /*build grid (individual cells).*/
Line 395 ⟶ 399:
end /*row*/
say
cText=.playfair(newXnewText, 1); say ' cyphercipher text=: ' digram(cText)
pText=.playfair(cText,0 ); say ' plain text=: ' digram(pText)
fillqText=space(translate(fillpText,,newKxx),0) /*remove anychar cypherused charactersfor "doubles. "*/
if length(qText)\==length(pText) then say 'possible text: ' digram(qText)
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────one-line subroutines────────────────*/
Line 415 ⟶ 421:
return strip($)
/*──────────────────────────────────.PLAYFAIR subroutine────────────────*/
.playfair: parse arg T,encrypt; i=-1; if encrypt==1 then i=1; $=
do k=1 while encrypti==1; _=substr(T,k,1); if _='' then leave
if _==substr(T,k+1,1) then T=left(T,k) || 'X'xx || substr(T,k+1)
end /*k*/
do j=1 by 2 to length(T); __=strip(substr(T,j,2)); call LR
if length(__)==1 then __=__ || 'X'xx; call LR /*append an "X"|Q characterchar, rule 1*/
select
when rowL==rowR then __=@@(rowL, colL+i)@@(rowR, colR+i) /*rule 2*/
Line 431 ⟶ 437:
'''output''' when using the default inputs:
<pre>
old cypher=cipher: Playfair example. ◄───the default.
new cypher=cipher: PLAYFIREXM
omit char : J
old phrase= Hide the gold in the tree stump!!
double char : X
new phrase= HIDETHEGOLDINTHETREESTUMP
newold digram=text HI DE: TH EGHide OLthe DIgold NTin HEthe TRtree EE ST UM Pstump!!
new phrase=text : HIDETHEGOLDINTHETREESTUMP
digram text ↔: HI DE TH EG OL DI NT HE TR EE ST UM P
 
cyphercipher text=: BM OD ZB XD NA BE KU DM UI XM MO UV IF
plain text=: HI DE TH EG OL DI NT HE TR EX ES TU MP
possible text: HI DE TH EG OL DI NT HE TR EE ST UM P
</pre>
After the usual replacements for $, @, #, and x= I ran the program on ooRexx with the following correct results: