RosettaCodeData/Task/Vigen-re-cipher/Common-Lisp/vigen-re-cipher-2.lisp

13 lines
405 B
Common Lisp
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
(defconstant +a+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(defun strip (s)
(string-upcase (remove-if-not 'alpha-char-p s)))
(defun vigenere (txt key &key plain &aux (p (if plain -1 1)))
(let ((txt (strip txt)) (key (strip key)) (i -1))
(map 'string
(lambda (c)
(setf i (mod (1+ i) (length key)))
(char +a+ (mod (+ (position c +a+) (* p (position (elt key i) +a+))) 26)))
txt)))