Write a program using Common Lisp that encrypts (encode)and decrypts (decode) a
ID: 3778202 • Letter: W
Question
Write a program using Common Lisp that encrypts (encode)and decrypts (decode) a phrase using the Caesar cipher.First convert all characters to numbers, A = 0, B = 1, C = 2, ..., Z = 25. The Caesar encryption function e(x), where x is the character being encrypted, is represented as e(x) = (x + k) (mod 26). k is the shift or key applied to each letter. After the function is applied the resulting number must then be converted back to a letter. The decryption function is e(x) = (x - k) (mod 26). Function Guidelines: 1) Function may only use recursion, 2) Each word of input must be read as a sub list of the list, and 3) No arrays, loops, or variables
Explanation / Answer
(defun EncryptionDecryptionText (ch KeyValue)
(let* ((c (char-code ch)) (la (char-code #)) (ua (char-code #A))
(base (cond ((<= la c (char-code #z)) la)
((<= ua c (char-code #Z)) ua)
(nil))))
(if base (code-char (+ (mod (+ (- c base) KeyValue) 26) base)) ch)))
(defun CodeEncryption (str KeyValue)
(map 'string #'(lambda (c) (EncryptionDecryptionText c KeyValue)) str))
(defun CodeDecryption (str KeyValue) (CodeEncryption str (- KeyValue)))
(let* ((OriginalText "Caesar cipher")
(KeyValue 5)
(EncryptedText (CodeEncryption OriginalText KeyValue))
(DecryptedText (CodeDecryption EncryptedText KeyValue)))
(format t "Original String: ~a ~%" OriginalText)
(format t "Encrypted String: ~a ~%" EncryptedText)
(format t "Decrypted String: ~a ~%" DecryptedText))
The above code will give the below Output:
Original String: Caesar cipher
Encrypted String: Hfjxfw hnumjw
Decrypted String: Caesar cipher
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.