Playfair cipher: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
m (→‎{{header|REXX}}: shortened a few statements. -- ~~~~)
Line 374: Line 374:
if \isLet(noX) then call err '"omitted" letter must be a Latin alphabet letter.'
if \isLet(noX) then call err '"omitted" letter must be a Latin alphabet letter.'
if pos(noX,oldX)\==0 then call err 'PHRASE can''t contain the "OMIT" character: ' noX
if pos(noX,oldX)\==0 then call err 'PHRASE can''t contain the "OMIT" character: ' noX
fill=space(translate('ABCDEFGHIJKLMNOPQRSTUVWXYZ',,noX),0)
fill=space(translate('ABCDEFGHIJKLMNOPQRSTUVWXYZ',,noX),0) /*remove NOX*/
/* [↑] remove the NOX character*/
newK= /* [↓] remove any duplicate chars*/
newK= /* [↓] remove any duplicate chars*/
do j=1 for length(oldK); _=substr(oldK,j,1)
do j=1 for length(oldK); _=substr(oldK,j,1)
Line 383: Line 382:
grid=left(newK || fill,26) /*use only the first 25 chars. */
grid=left(newK || fill,26) /*use only the first 25 chars. */
say ' new cypher=' newK
say ' new cypher=' newK
say ' grid fill=' fill
say ' grid text=' grid
say ' old phrase=' oldX
say ' old phrase=' oldX
say ' new phrase=' newX
say ' new phrase=' newX
say ' new digram=' digram(newX)
say ' new digram=' digram(newX)
#=0
#=0
do row =1 for 5 /*build grid (individual cells).*/
do row =1 for 5 /*build grid (individual cells).*/
do col=1 for 5; #=#+1; @.row.col=substr(grid,#,1)
do col=1 for 5; #=#+1; @.row.col=substr(grid,#,1)
if row==1 then @.0.col=@.1.col
if row==1 then @.0.col=@.1.col
if col==5 then @.row.6=@.row.1
if col==5 then do; @.row.6=@.row.1; @.row.0=@.row.5; end
if row==5 then @.6.col=@.1.col
if row==5 then do; @.6.col=@.1.col; @.0.col=@.5.col; end
if row==5 then @.0.col=@.5.col
end /*col*/
if col==5 then @.row.0=@.row.5
end /*row*/
end /*col*/
end /*row*/
say
say
cText=.playfair(newX, 1); say ' cypher text=' digram(cText)
cText=.playfair(newX, 1); say ' cypher text=' digram(cText)
Line 426: Line 421:
if _==substr(T,k+1,1) then T=left(T,k) || 'X' || substr(T,k+1)
if _==substr(T,k+1,1) then T=left(T,k) || 'X' || substr(T,k+1)
end /*k*/
end /*k*/

do j=1 by 2 to length(T); __=strip(substr(T,j,2)); call LR
do j=1 by 2 to length(T); __=strip(substr(T,j,2)); call LR
if len==1 | L==R then __=L || 'X' /*append an "X" character, rule 1*/
if len==1 | L==R then __=L || 'X' /*append an "X" character, rule 1*/