Anonymous user
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
parse arg key . 1 oldK
if
if
if
key =scrub(key) /*use only Latin alphabet letters*/
say ' old cypher=' oldK▼
if _\=='' then call err 'too many arguments specified.'
if
if length(
if \datatype(
if pos(
fill=space(translate('ABCDEFGHIJKLMNOPQRSTUVWXYZ',,
▲newK= /* [↓] remove any duplicate chars*/
newKey=
if pos(_,newKey)==0 then newKey=newKey || _
end /*j*/
xx='X'; if omit==xx then xx='Q' /*char used for double characters*/
if length(
fill=space(translate(fill,,newK),0) /*remove any cypher characters. */▼
grid=left(newKey || fill,26) /*use only the first 25 chars. */
say ' new cypher=' newK▼
say ' old
say ' new
say '
say 'double char : ' xx
say 'digram text ↔: ' digram(newText)
#=0
do row =1 for 5 /*build grid (individual cells).*/
Line 395 ⟶ 399:
end /*row*/
say
cText=.playfair(
pText=.playfair(cText
▲
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
if _==substr(T,k+1,1) then T=left(T,k) ||
end /*k*/
do j=1 by 2 to length(T); __=strip(substr(T,j,2)); call LR
if length(__)==1 then __=__ ||
select
when rowL==rowR then __=@@(rowL, colL+i)@@(rowR, colR+i) /*rule 2*/
Line 431 ⟶ 437:
'''output''' when using the default inputs:
<pre>
old
new
omit char : J
double char : X
new phrase= HIDETHEGOLDINTHETREESTUMP▼
digram text ↔: HI DE TH EG OL DI NT HE TR EE ST UM P
plain text
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:
|