RosettaCodeData/Task/Vigen-re-cipher/Common-Lisp/vigen-re-cipher-2.lisp
2023-07-01 13:44:08 -04:00

12 lines
405 B
Common Lisp

(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)))