Caesar cipher: Difference between revisions

Content added Content deleted
Line 1,891: Line 1,891:
"The quick brown fox jumps over the lazy dog"</pre>
"The quick brown fox jumps over the lazy dog"</pre>


====Variant====
====Alternate version====
1. Note
1. Note
<pre>if rot(text shift) = cyphertext then rot(cyphertext -shift) = text</pre>
<pre>if caesar(txt offset) = cyphertext then caesar(cyphertext -offset) = txt</pre>

See « caesar-decipher » function above.


2. Program
2. Program


<lang lisp>(defconstant +rot+ "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz")
<lang lisp>(defconstant +a+ "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz")
(defun rot (text shift)
(defun caesar (txt offset)
(map 'string
(map 'string
#'(lambda (x)
#'(lambda (x)
(if (find x +rot+)
(if (find x +a+)
(char +rot+ (mod (+ (position x +rot+) (* 2 shift)) 52))
(char +a+ (mod (+ (position x +a+) (* 2 offset)) 52))
x))
x))
text))</lang>
txt))</lang>


3. Execution
3. Execution

See 1.


{{out}}
{{out}}
<pre>(rot "Ave Caesar morituri te salutant" 13)
<pre>(caesar "Ave Caesar morituri te salutant" 13)
Nir Pnrfne zbevghev gr fnyhgnag
Nir Pnrfne zbevghev gr fnyhgnag
(rot "Nir Pnrfne zbevghev gr fnyhgnag" -13)
(caesar "Nir Pnrfne zbevghev gr fnyhgnag" -13)
Ave Caesar morituri te salutant</pre>
Ave Caesar morituri te salutant</pre>