Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-1.clj
Normal file
29
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-1.clj
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(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))
|
||||
11
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-2.clj
Normal file
11
Task/Vigen-re-cipher/Clojure/vigen-re-cipher-2.clj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(ns org.rosettacode.clojure.test-vigenere
|
||||
(:require [org.rosettacode.clojure.vigenere :as vigenere]))
|
||||
|
||||
(let
|
||||
[ plaintext "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
|
||||
key "Vigenere cipher"
|
||||
ciphertext (vigenere/encrypt plaintext key)
|
||||
recovered (vigenere/decrypt ciphertext key) ]
|
||||
|
||||
(doall (map (fn [[k v]] (printf "%9s: %s\n" k v))
|
||||
[ ["Original" plaintext] ["Key" key] ["Encrypted" ciphertext] ["Decrypted" recovered] ])))
|
||||
Loading…
Add table
Add a link
Reference in a new issue