Advertisement
Guest User

caesar

a guest
Nov 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.07 KB | None | 0 0
  1. ;This function does most of the work!
  2. (defun encipher-char (ch shift)
  3.   (let* ((c  (char-code  ch)) (ua (char-code #\A))
  4.          (base (cond ((<= ua c (char-code #\Z)) ua)  
  5.                      (nil))))
  6.     (if base (code-char (+ (mod (+ (- c base) shift) 26) base)) ch)))
  7.    
  8. ;Encrypt the plaintext
  9. (defun caesar-cipher (str shift)
  10.   (map 'string #'(lambda (c) (encipher-char c shift)) str))
  11.  
  12. ;Decrypt the plaintext
  13. (defun caesar-decipher (str shift) (caesar-cipher str (- shift)))
  14.  
  15. ;Break the cipher
  16. (defun caesar-solve (str maxShift)
  17.   (loop for i from 0 to maxShift
  18.     do (let* ((solved (caesar-cipher plaintext i)))
  19.        (format t "Solved: ~a ~%" solved))))
  20.  
  21. ;Define some variables and print some results
  22. (let* ((plaintext(string-upcase "abc"))
  23.        (shift 1)
  24.        (maxShift 1)
  25.        (encrypted (caesar-cipher plaintext shift))
  26.        (decrypted (caesar-decipher encrypted shift))
  27.        (solved (caesar-solve plaintext 26)))
  28.   (format t " Original: ~a ~%" plaintext)
  29.   (format t "Encrypted: ~a ~%" encrypted)
  30.   (format t "Decrypted: ~a ~%" decrypted))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement