In article <··············@bobo.spike.home>, ···········@charter.net
wrote:
> Hi all,
>
> I've been trying to teach myself lisp for a while now. Below is
> some lisp code to encrypt a message using the RC4 algorithm. It
> is basically a literal translation of the C code for RC4 encryption
> available on the internet. It produces the same results as the
> test vectors given with the C code, so I believe it is correct.
>
> What I'd like to know is, is there a 'better' way to write this.
> Something that looks less like C code.
Maybe there is. You could use some more built-in functionality
like DOTIMES, LOOP and ROTATEF. Example:
(defun make-new-rc4-codebook (key)
"Return an RC4 codebook initialized with key.
Key is 1 to 256 8 bit integers"
(let ((codebook (make-array '(256) :initial-element 0))
(key-length (length key)))
(dotimes (n 256)
(setf (aref codebook n) n))
(loop for n below 256
for index1 = 0 then (mod (1+ index1) key-length)
with index2 = 0
do (setf index2 (mod (+ (aref key index1)
(aref codebook n)
index2)
256))
do (rotatef (aref codebook n) (aref codebook index2)))
codebook))
But this really doesn't change that much.
> (defun run-tests ()
> "Run the tests"
For experimenting with algorithms it is sometimes
quite handy to use a regression tester.
; deftest NAME FORM &rest VALUES
(rt:deftest "test1"
(rc4-crypt (make-array '(8) :initial-contents '(#x1 #x23 #x45 #x67 #x89 #xab #xcd #xef))
(make-array '(8) :initial-contents '(#x1 #x23 #x45 #x67 #x89 #xab #xcd #xef)))
#(#x75 #xb7 #x87 #x80 #x99 #xe0 #xc5 #x96))
(rt:do-tests)
You would need to modify the "RT" regression tester from
the CMU Lisp archive for your purpose.
--
Rainer Joswig, Hamburg, Germany
Email: ·············@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/