Anonymous user
Playfair cipher: Difference between revisions
m
→{{header|REXX}}: split some compound statements, added/changed whitespace and comments, ordered functions by name, used a template for the output sections.
m (→{{header|REXX}}: split some compound statements, added/changed whitespace and comments, ordered functions by name, used a template for the output sections.) |
|||
Line 2,088:
<br>A fair amount of code was added to massage the decrypted encryption to remove doubled '''X'''es so as to match the original text
<br>(this is the ''possible text'' part of the REXX code).
<lang rexx>/*REXX program implements a PLAYFAIR cipher (encryption
@abc= 'abcdefghijklmnopqrstuvwxyz'; @abcU= @abc
parse arg omit key '(' text /*TEXT is the phrase to be used.
if
if
newKey = scrub(key, 1) /*scrub old cipher key ──► newKey */
if length(omit)\==1 then call err 'OMIT letter must be only one letter'▼
newText= scrub(text ) /* " " text ──► newText */
▲if length(omit)\==1 then call err 'OMIT letter must be only one letter.'
cant='can''t contain the "OMIT" character: ' omit▼
if
fill=space(translate(@abcU,, omit), 0) /*elide OMIT char from alphabet. */▼
if
fill= space( translate(
if omit==xx then xx= 'Q' /* " " " " " */
say 'old cipher key: ' strip(oldK) ; padL=14+2; pad=left('',padL)▼
if length(newKey)<3 then call err ,
say 'new cipher key: ' newKey ; padX=left('',padL,"═")'Playfair'▼
▲fill= space( translate(
grid= newKey || fill /*only first 25 characters are used. */
say ' original text: ' strip(text) /* [↓] doubled version of Lxx. */▼
call show 'cleansed', newText ; LxxLxx=Lxx || Lxx▼
say 'new cipher key: ' newKey
▲#=0 /*number of grid characters used.*/
say ' omit char: ' omit
do row =1 for 5 /*build array of individual cells*/▼
say ' double char: ' xx
do col=1 for 5; #=#+1; @.row.col=substr(grid,#,1)▼
if row==1 then @.0.col=@.1.col▼
padL= 14 + 2
#=0
do row =1 for 5
eText=.Playfair(newText, 1); call show 'encrypted' , eText▼
qText=changestr(xx ||xx,pText,Lxx) /*change doubled doublechar─►sing*/▼
qText=changestr(Lxx||xx,qText,LxxLxx) /*change Xx ──► lowercase dblChar*/▼
qText=space(translate(qText,,xx),0) /*remove char used for "doubles."*/▼
pad = left('', padL)
▲upper qText /*reinstate the use of upperchars*/
if length(qText)\==length(pText) then call show 'possible', qText▼
Lxx = translate(xx, @abc, @abcU) /* [↓] lowercase of double character. */
say ' original text: ' newText; say /*··· and show the original text.*/▼
LxxLxx= Lxx || Lxx /* [↓] doubled version of Lxx. */
pText= .Playfair(eText ); call show 'plain' , pText
upper qText /*reinstate the use of upper characters*/
▲if length(qText)\==length(pText) then call show 'possible', qText
▲say ' original text: ' newText; say /*··· and also show the original text. */
if qtext==newText then say padx 'encryption──► decryption──► encryption worked.'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
@@: parse arg Xrow,Xcol; return @.Xrow.Xcol
err: say; say '***error!***' arg(1); say; exit 13
LR: rowL= row(left(__, 1)); colL= _; rowR= row(right(__,1)); colR= _; return length(__)
row: ?= pos(arg(1), grid);
show: arg ,y; say; say right(arg(1) 'text: ',padL) digram(y); say pad space(y, 0); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
.Playfair: arg T,encrypt; i= -1; if encrypt==1 then i= 1; $=
scrub: procedure; arg xxx,unique; xxx=space(xxx,0) /*ARG caps all args*/▼
if
do j=1 by 2 to length(T); __= strip( substr(T, j, 2) )
return $▼
if LR()==1 then __= __ || xx; call LR /*append X or Q char, rule 1*/
when rowL==rowR then __= @@(rowL, colL+i)@@(rowR, colR+i) /*rule
when colL==colR then __= @@(rowL+i, colL )@@(rowR+i, colR)
return strip($)▼
end /*select*/
▲ return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲ end /*k*/
digram: procedure; parse arg x; $=; do j=1 by 2 to length(x)
▲ return strip($)
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲ when rowL==rowR then __=@@(rowL, colL+i)@@(rowR, colR+i) /*rule 2*/
if unique==1 then if pos(_, $)\==0 then iterate /*is unique?*/
▲ end /*select*/
if datatype(_, 'M') then $= $ || _ /*only use Latin letters. */
end /*j*/
return $</lang>
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]].
<br><br>
<pre>
old cipher key: Playfair example. ◄───using the default.
Line 2,192 ⟶ 2,201:
════════════════Playfair encryption──► decryption──► encryption worked.
</pre>
<pre>
old cipher key: stuvw
|