Skip to content

Instantly share code, notes, and snippets.

@codepony
Last active December 11, 2015 03:58
Show Gist options
  • Save codepony/4541659 to your computer and use it in GitHub Desktop.
Save codepony/4541659 to your computer and use it in GitHub Desktop.
Substitution cipher written in CLISP.
#|
| Subtitution cipher written in CLISP
| - used rot13.lisp as core here - (https://gist.github.com/4530170)
|
ceasars_cipher.lisp version 0.0.1
Copyright (C) 2012 @d3f (http://identi.ca/d3f)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| How-to: (load "ceasars_cipher.lisp") & (startup) to run it.
| The two letters you type in can be anything, not even letters.
|#
(defparameter code-list (list))
(defparameter char-list (list))
(defparameter sub_number 0)
(defun startup ()
(format t "~%Type in the two letters you want to have substituted. (`quit' will exit)~%")
(let ((str (read-line)))
(cond
((equal str "quit") (format t "Good Bye ~%"))
(t (my_substitute str))
))
)
(defun my_substitute (str)
(let ((letter_a (car (concatenate 'list str))) (letter_b (cadr (concatenate 'list str))))
(let ((num_a (char-code letter_a)) (num_b (char-code letter_b)))
(cond
((or (and (and (>= num_a 97) (<= num_a 122)) (and (>= num_b 97) (<= num_b 122))) (and (and (>= num_a 65) (<= num_a 90)) (and (>= num_b 65) (<= num_b 90))))
(setf sub_number (- (char-code letter_b) (char-code letter_a)))
(main))
(t (format t "You should type in two same-typ LETTERS!~%") (startup))
)))
)
(defun main ()
(format t "~%Type in the String you want to have encyrpted (`quit' will exit / `letters' will let you change the letters)~%")
(let ((str (read-line)))
(cond
((equal str "quit") (format t "Good Bye ~%"))
((equal str "letters") (startup))
(t (encode str) (main))
))
)
(defun encode (str)
(map 'list #'make-code-list str)
(map 'list #'make-char-list code-list)
(format t "Your String after using ceasars_cipher: ~% ~a ~%" (concatenate 'string char-list))
(setf code-list (list))
(setf char-list (list))
)
(defun make-code-list (char)
(setf code-list (cons (char-code char) code-list))
)
(defun make-char-list (code)
(when (and (>= code 97) (<= code 122))
(setf code (+ code sub_number))
(if (> code 122)
(decf code 26)
)
(if (< code 97)
(incf code 26)
))
(when (and (>= code 65) (<= code 90))
(setf code (+ code sub_number))
(if (> code 90)
(decf code 26)
)
(if (< code 65)
(incf code 26)
))
(setf char-list (cons (code-char code) char-list))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment