public
Created

RC4 (e.g. ARCFOUR) in (Guile) Scheme. Yes, this is vulnerable in many ways, since it's straight up RC4.

  • Download Gist
rc4.scm
Scheme
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
;;; RC4, just for the hell of it.
;;; Written for Guile 2.0+
;;; Use like so:
;;; Encypher: (rc4 "Key" (map char->integer (string->list "Plaintext")))
;;; $1 = (187 243 22 232 217 64 175 10 211)
;;; Decypher: (rc4 "Key" $1)
;;; $2 = (80 108 97 105 110 116 101 120 116)
 
(define (rc4-make-schedule key)
(let ((L (string-length key)))
(let loop ((K (apply vector (map char->integer (string->list key))))
(S (apply vector (iota 256)))
(i 0)
(j 0))
(if (< i 256)
(begin
(set! j (remainder (+ j
(vector-ref S i)
(vector-ref K (remainder i L)))
256))
(let ((t (vector-ref S i)))
(vector-set! S i (vector-ref S j))
(vector-set! S j t)
(loop K S (+ i 1) j)))
S))))
 
(define (rc4-make-generator S)
(let ((i 0)
(j 0))
(lambda ()
(set! i (remainder (+ i 1) 256))
(set! j (remainder (+ j (vector-ref S i)) 256))
(let ((t (vector-ref S i)))
(vector-set! S i (vector-ref S j))
(vector-set! S j t))
(vector-ref S (remainder
(+ (vector-ref S i) (vector-ref S j))
256)))))
 
(define (rc4 K P)
(let* ((S (rc4-make-schedule K))
(G (rc4-make-generator S)))
(map (lambda (x)
(logxor (G) x))
P)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.