Task

Implement a   Vigenère cypher,   both encryption and decryption.

The program should handle keys and text of unequal length,
and should capitalize everything and discard non-alphabetic characters. 

(If your program handles non-alphabetic characters in another way,
make a note of it.)



Related tasks

   Caesar cipher
   Rot-13
   Substitution Cipher


(ns org.rosettacode.clojure.vigenere
  (:require [clojure.string :as string]))
 
; convert letter to offset from \A
(defn to-num [char] (- (int char) (int \A)))
 
; convert number to letter, treating it as modulo 26 offset from \A
(defn from-num [num] (char (+ (mod num 26) (int \A))))
 
; Convert a string to a sequence of just the letters as uppercase chars
(defn to-normalized-seq [str]
  (map #'first (re-seq #"[A-Z]" (string/upper-case str))))
 
; add (op=+) or subtract (op=-) the numerical value of the key letter from the
; text letter.  
(defn crypt1 [op text key] 
  (from-num (apply op (list (to-num text) (to-num key)))))
 
(defn crypt [op text key]
  (let [xcrypt1 (partial #'crypt1 op)]
    (apply #'str
      (map xcrypt1 (to-normalized-seq text) 
                   (cycle (to-normalized-seq key))))))
 
; encipher a text
(defn encrypt [plaintext key] (crypt #'+ plaintext key))
 
; decipher a text
(defn decrypt [ciphertext key] (crypt #'- ciphertext key))