Vigenère cipher: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured) |
Cyril Nocton (talk | contribs) |
||
Line 914: | Line 914: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
====Main solution==== |
|||
This doesn't assume anything about character codes other than A-Z being a contiguous block (but still, we could be using EBCDIC. Who knows.) |
This doesn't assume anything about character codes other than A-Z being a contiguous block (but still, we could be using EBCDIC. Who knows.) |
||
<lang lisp>(defun strip (s) |
<lang lisp>(defun strip (s) |
||
Line 945: | Line 946: | ||
enc: WMCEEIKLGRPIFVMEUGXXYILILZXYVBZLRGCEYAIOEKXIZGU |
enc: WMCEEIKLGRPIFVMEUGXXYILILZXYVBZLRGCEYAIOEKXIZGU |
||
dec: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH</pre> |
dec: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH</pre> |
||
====Alternative==== |
|||
No string to circular list conversion. |
|||
1. Program |
|||
<lang lisp>(defconstant +a+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ") |
|||
(defun strip (s) |
|||
(string-upcase (remove-if-not 'alpha-char-p s))) |
|||
(defun vigenere (text key &key decode &aux (d (if decode -1 1))) |
|||
(let ((text (strip text)) (key (strip key)) (i -1)) |
|||
(map 'string |
|||
(lambda (x) |
|||
(setf i (mod (1+ i) (length key))) |
|||
(char +a+ (mod (+ (position x +a+) (* d (position (elt key i) +a+))) 26))) |
|||
text)))</lang> |
|||
2. Execution |
|||
{{out}} |
|||
<pre>(vigenere "« Through the Looking-Glass »" "Lewis Carroll") |
|||
"ELNWMIHKYSWZZOEVYILRJG" |
|||
(vigenere "ELNWMIHKYSWZZOEVYILRJG" "Lewis Carroll" :decode t) |
|||
"THROUGHTHELOOKINGGLASS"</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |