I was thinking too strictly in terms of the already existing functions so I didn't notice the algebraic error, silly me. Well, the nice thing is that I now have a fully functional program. This is the complete project:
Code: Select all
; Encryption-Project by Cara Petrovitsch
; 1) Program that takes in a word(given as a string) plus a number and encrypts the word using Caesar's Cipher in the modulo given as the number:
; Extended alphabet as parameter plus one white space at index 0:
(defparameter *alphabet* " aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ,0.1?2!3;4:5@6-7_8'9")
; Finds the index of char in *alphabet*:
(defun alphabet-index (char)
(position char *alphabet*))
; Finds the char shifted n places to the right in the alphabet:
(defun rotate (char n)
(elt *alphabet* (mod (+ (alphabet-index char) n)(length *alphabet*))))
; Makes the rotate function easier to use with map:
(defun e-helper (n)
#'(lambda (char)
(rotate char n)))
; Returns the encrypted string (when no modulo is specified explicitly, modulo 7 is used):
(defun c-encrypt (str &optional (n 7))
(map 'string (e-helper n) str))
; 2) Program that decrypts an already encrypted word given as a string (first argument) using Caesar's cipher in the modulo given (second argument)
; Rotates the encrypted chars back to their original meaning:
(defun rotate-back (char n)
(elt *alphabet* (mod (- (alphabet-index char) n)(length *alphabet*))))
; Makes the rotate-back function easier to use with map:
(defun d-helper (n)
#'(lambda (char)
(rotate-back char n)))
; Returns the decrypted string (when no modulo is specified explicitly, modulo 7 is used):
(defun c-decrypt (str &optional (n 7))
(map 'string (d-helper n) str))
; 3) Program that takes a plaintext and a keyword (given in stringformat) and encrypts the plaintext using Vigenere cipher
; Function that rotates the characters of the plaintext modulo the key's index:
(defun v-rotate (P K)
(rotate P (alphabet-index K))))
; Returns the encrypted string:
(defun v-encrypt (string key)
(coerce (loop for i from 0 to (1- (length string))
collect (v-rotate (char string i) (char key (mod i (length key)))))
'string))
; 4) Program that takes a ciphertext and a keyword (given in stringformat) and decrypts the ciphertext using Vigenere cipher
; Function that rotates the Ciphertexts char back to its Plaintext position:
(defun v-rotate-back (C K)
(rotate-back C (alphabet-index K)))
; returns the decrypted string:
(defun v-decrypt (string key)
(coerce (loop for i from 0 to (1- (length string))
collect (v-rotate-back (char string i) (char key (mod i (length key)))))
'string))
Thank you once again! (I start sounding like a scratched plate... don't I?)