RSA code: Difference between revisions

152 bytes added ,  10 years ago
m
(Added Common Lisp)
Line 39:
 
=={{header|Common Lisp}}==
In this example, the functions encode-string and decode-string are responsible for converting the string to an integer (which can be encoded) and back. They are not very important to the RSA algorithm, which happens in encode-rsa, decode-rsa, and mod-exp.
 
<lang Common Lisp>(defparameter *n* 9516311845790656153499716760847001433441357)
(defparameter *e* 65537)
Line 46 ⟶ 48:
;; each character and subtracting 22, making SPACE=10 and so on. Only
;; ASCII values 32 - 121 can be represented. Who needs lowercase z anyway.
 
;; The next two functions convert the string to an integer and back.
;; They aren't really that important. You can really do whatever you want.
 
;; magic
Line 54 ⟶ 53:
(parse-integer (reduce #'(lambda (x y) (concatenate 'string x y))
(loop for c across message collect (write-to-string (- (char-code c) 22))))))
 
;; sorcery
(defun decode-string (message) (coerce (loop for (a b) on
(loop for char across (write-to-string message) collect char)
by #'cddr collect (code-char (+ (parse-integer (coerce (list a b) 'string)) 22))) 'string))
 
;; ACTUAL RSA ALGORITHM STARTS HERE ;;
 
;; fast modular exponentiation: runs in O(log exponent)