Straddling checkerboard: Difference between revisions

Content added Content deleted
m (added whitespace before the TOC, added a ;Task (bold) header.)
(→‎{{header|REXX}}: added the REXX language.)
Line 1,590: Line 1,590:
450582425181653945125016505180125423293721256216286286288653970163758524
450582425181653945125016505180125423293721256216286286288653970163758524
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre>
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre>

=={{header|REXX}}==
Extra coding was added to:
* &nbsp; allow the use of any table &nbsp; (specifiable on the invocation of the &nbsp; '''genCipher''' &nbsp; subroutine, the 5<sup>th</sup> line in the REXX program),
* &nbsp; no hard-coding of the location of blanks,
* &nbsp; no hard-coding or the numbered lines in the straddled checkerboard table,
* &nbsp; no hard-coding of the location of the escape character,
* &nbsp; 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. */
arg msg /*obtain optional message from the C.L.*/
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 /*encrypt message and show encryption. */
dec=decrypt(enc); say ' decrypted=' dec /*decrypt " " " decryption. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
genCipher: @.=; arg @..,two,three; z=-1; @.z= @.. /*build top row of cipher.*/
_=pos(' ', @.. ) - 1; @._= two /* " 2nd " " " */
_=pos(' ', @.., _+2) - 1; @._= three /* " 3rd " " " */
do j=0 for 9; @..=@.. || @.j /*construct a table for fast searching.*/
if @.j\=='' then @.r=@.r || j
_=pos('/', @.j) /*is the escape character in this row? */
if _\==0 then @.dig=j || (_-1) /*define " " for numerals.*/
end /*j*/
@..=space(@.., 0) /*purify the table of encryptable chars*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
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) /*obtain a message char to be encrypted*/
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) /*traipse thru rows, looking for match.*/
if y==0 then iterate /*Not in this row? Then keep looking.*/
z=k; if z==-1 then z= /*construct the index of the cypher row*/
$=$ || z || (y-1) /*add an encrypted character to output.*/
iterate j /*go and process the next msg character*/
end /*k*/
end /*j*/
return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
decrypt: procedure expose @.; parse arg !,,$ /*$: output (decrypted text) so far.*/
do j=1 to length(!); rw=-1 /*process each of the encypted numbers.*/
x=substr(!,j,1) /*obtain a message char to be decrypted*/
if substr(!,j,2)==@.dig then do; j=j+2; $=$ || substr(!, j, 1); iterate; end
if pos(x, @.r)\==0 then do; j=j+1; rw=x; x=substr(!, j, 1); end
$=$ || substr(@.rw, x+1, 1) /*add a character to decrypted message.*/
end /*j*/
return $</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
plain text= One night-it was the twentieth of March, 1888-I was returning
encrypted= 4505824251816539125016505180125423293721256216286286288653970163758524
decrypted= ONENIGHTITWASTHETWENTIETHOFMARCH1888IWASRETURNING
</pre>





=={{header|Ruby}}==
=={{header|Ruby}}==