Anonymous user
Playfair cipher: Difference between revisions
→{{header|REXX}}: changed variable names and better showing of input and output, choosing a better "double character", added a conclusion (output is Playfair encryption is correct). -- ~~~~
m (→{{header|REXX}}: changed direction of an arrow in a comment. -- ~~~~) |
(→{{header|REXX}}: changed variable names and better showing of input and output, choosing a better "double character", added a conclusion (output is Playfair encryption is correct). -- ~~~~) |
||
Line 360:
=={{header|REXX}}==
Quite a bit of the REXX code deals with error checking, accepting arguments, and displaying the options used, and displaying input and output.
<br><br>Thanks to Walter Pachl, this program is now sensitive of using a suitable ''double character'' when '''x''' is present in the cipher key.
<lang rexx>/*REXX program implements a PLAYFAIR cipher (encryption & decryption).*/
parse arg key . 1 oldK omit _ . '(' text /*TEXT is the phrase to be used*/
if key =='' | key ==',' then do; key='Playfair example.'; oldK=key "
if omit=='' | omit==',' then omit='J' /*the "omitted" character. */
if text='' then text='Hide the gold in the tree stump!!' /*default.*/
Line 375 ⟶ 376:
upper omit /*uppercase the OMIT character.*/
fill=space(translate('ABCDEFGHIJKLMNOPQRSTUVWXYZ',,omit),0) /*elide omit*/
xx='X'; if omit==xx then xx='Q'
if length(newKey)<3 then call err 'cipher key is too short, must be > 2 unique characters.'
fill=space(translate(fill,,newKey),0) /*remove any cipher characters. */
grid=left(newKey || fill,26) /*use only the first 25 chars. */
say ' old cipher: ' strip(oldK) ; padL=14+2; pad=left('',padL)
say ' new cipher: ' newKey ; padX=left('',padL,'═')
say '
say '
say '
say '
#=0
do row =1 for 5 /*build grid (individual cells).*/
Line 394:
end /*col*/
end /*row*/
cText=.playfair(newText, 1);
pText=.playfair(cText );
qText=space(translate(pText,,xx),0) /*remove char used for "doubles."*/
if length(qText)\==length(pText) then
say ' original text: ' newText; say /*··· and show the original text.*/
if qtext==newText then say padx 'Playfair encryption─►decryption─►encryption worked.'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────one─line subroutines───────────────────────────*/
@@:
err:
LR:
row:
show: arg x,y; say; say right(x 'text: ',padL) digram(y); say pad space(y,0); return
/*──────────────────────────────────SCRUB subroutine────────────────────*/
scrub:
$=; do j=1 for length(xxx); _=substr(xxx,j,1)
if unique==1
if datatype(_,'M')
end /*j*/▼
return $
/*──────────────────────────────────DIGRAM subroutine───────────────────*/
digram: procedure; parse arg x; $=;
return strip($)
/*──────────────────────────────────.PLAYFAIR subroutine────────────────*/
.playfair:
do k=1 while i==1; _=substr(T,k,1); if _==' ' then leave
if _==substr(T,k+1 ,1) then T=left(T,k) || xx || substr(T,k+1)
end /*k*/
do j=1 by 2 to length(T); __=strip(substr(T,j,2))
if
select
when rowL==rowR then __=@@(rowL, colL+i)@@(rowR, colR+i) /*rule 2*/
Line 435 ⟶ 437:
'''output''' when using the default inputs:
<pre>
old cipher: Playfair example.
new cipher: PLAYFIREXM
digram text ↔: HI DE TH EG OL DI NT HE TR EE ST UM P▼
HIDETHEGOLDINTHETREXESTUMP
HIDETHEGOLDINTHETREESTUMP
original text: HIDETHEGOLDINTHETREESTUMP
════════════════ Playfair encryption─►decryption─►encryption worked.
</pre>
After the usual replacements for $, @, #, and x= I ran the program on ooRexx with the following correct results:
Line 455 ⟶ 464:
cypher 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
</pre>
'''output''' when using the input of: <tt> stuvw x (myteest </tt>
<pre>
old cipher: stuvw
new cipher: STUVW
omit char: X
double char: Q
original text: myteest
new text: MYTEEST
CIPHER text: NR WB ZB TU
NRWBZBTU
PLAIN text: MY TE QE ST
MYTEQEST
POSSIBLE text: MY TE ES T
original text: MYTEEST
════════════════ Playfair encryption─►decryption─►encryption worked.
</pre>
=={{header|Tcl}}==
|