Straddling checkerboard: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
m (→{{header|REXX}}: added whitespace.) |
||
Line 2,337:
:* support the usage of a blank in the 1<sup>st</sup> character (the top line of the table).
<lang rexx>/*REXX program uses the straddling checkerboard cipher to encrypt/decrypt a message. */
parse arg msg
if msg='' then msg= 'One night-it was the twentieth of March, 1888-I was returning'
say 'plain text=' msg
call genCipher 'et aon ris', 'bcdfghjklm', 'pq/uvwxyz.' /*build the cipher (board)*/
enc= encrypt(msg); say ' encrypted=' enc
dec= decrypt(enc); say ' decrypted=' dec
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
genCipher: @.=; arg @..,two,three; z= -1; @.z= @..
_= pos(' ', @.. ) - 1; @._= two
_= pos(' ', @.., _+2) - 1; @._= three
do j=0 for 9; @..= @.. || @.j
if @.j\=='' then @.r= @.r || j
_= pos('/', @.j)
if _\==0 then @.dig= j || (_-1)
end /*j*/
@..= space(@.., 0);
/*──────────────────────────────────────────────────────────────────────────────────────*/
encrypt: procedure expose @.; arg !,,$ /*$: output (encrypted text) so far.*/
do j=1 for length(!) /*process each of the plain─text chars.*/
x= substr(!, j, 1)
if datatype(x, 'W') then do; $= $ || @.dig || x; iterate; end /*numeral?
if pos(x, @..)==0 then iterate /*Not one of the allowable chars? Skip*/
do k=-1 for 10; y= pos(x, @.k)
if y==0 then iterate /*Not in this row? Then keep looking.*/
z= k;
$= $ || z || (y-1);
end /*k*/
end /*j*/; return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
decrypt: procedure expose @.; parse arg !,,$
do j=1 to length(!); rw= -1
if substr(!,j,2)==@.dig then do; j= j+2; $= $ ||
if pos(x, @.r)\==0 then do; j= j+1; rw=x; x=substr(!, j, 1);
$= $ || substr(@.rw, x+1, 1)
end /*j*/; return $</lang>
{{out|output|text= when using the default input:}}
<pre>
|