Caesar cipher: Difference between revisions
Content added Content deleted
Cyril Nocton (talk | contribs) |
Cyril Nocton (talk | contribs) |
||
Line 1,849: | Line 1,849: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
====W/ decipher function==== |
|||
<lang lisp>(defun encipher-char (ch key) |
<lang lisp>(defun encipher-char (ch key) |
||
(let* ((c (char-code ch)) (la (char-code #\a)) (ua (char-code #\A)) |
(let* ((c (char-code ch)) (la (char-code #\a)) (ua (char-code #\A)) |
||
Line 1,888: | Line 1,889: | ||
"Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald" |
"Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald" |
||
> (caesar-decipher "Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald" 23) |
> (caesar-decipher "Qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald" 23) |
||
"The quick brown fox jumps over the lazy dog" |
"The quick brown fox jumps over the lazy dog"</pre> |
||
====W/o decipher function==== |
|||
</pre> |
|||
1. Note |
|||
<pre>if rot(text shift) = cyphertext then rot(cyphertext -shift) = text</pre> |
|||
2. Program |
|||
Text manipulation is a real pain in the arse. Requiescat in pace. |
|||
<lang lisp>(defconstant +rot+ "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz") |
|||
(defun rot (text shift) |
|||
;; ask cyril:nocton@gmail.com |
|||
(map 'string |
|||
#'(lambda (x) |
|||
(if (find x +rot+) |
|||
(char +rot+ (mod (+ (position x +rot+) (* 2 shift)) 52)) |
|||
x)) |
|||
text))</lang> |
|||
3. Execution |
|||
See 1. |
|||
{{out}} |
|||
<pre>(rot "Ave Caesar morituri te salutant" 13) |
|||
Nir Pnrfne zbevghev gr fnyhgnag |
|||
(rot "Nir Pnrfne zbevghev gr fnyhgnag" -13) |
|||
Ave Caesar morituri te salutant</pre> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |