RosettaCodeData/Task/Vigen-re-cipher/Racket/vigen-re-cipher-1.rkt
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

21 lines
644 B
Racket

#lang racket
(define chr integer->char)
(define ord char->integer)
(define (encrypt msg key)
(define cleaned
(list->string
(for/list ([c (string-upcase msg)]
#:when (char-alphabetic? c)) c)))
(list->string
(for/list ([c cleaned] [k (in-cycle key)])
(chr (+ (modulo (+ (ord c) (ord k)) 26) (ord #\A))))))
(define (decrypt msg key)
(list->string
(for/list ([c msg] [k (in-cycle key)])
(chr (+ (modulo (- (ord c) (ord k)) 26) (ord #\A))))))
(decrypt (encrypt "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
"VIGENERECIPHER")
"VIGENERECIPHER")