Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (define alph (string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
- (define r1 (string->list "EKMFLGDQVZNTOWYHXUSPAIBRCJ"))
- (define r2 (string->list "AJDKSIRUXBLHWTMCQGZNPYFVOE"))
- (define r3 (string->list "BDFHJLCPRTXVZNYEIWGAKMUSQO"))
- (define r4 (string->list "ESOVPZJAYQUIRHXLNFTGKDCMWB"))
- (define r5 (string->list "VZBRGITYUPSDNHLXAWMJQOFECK"))
- (define refB (string->list "YRUHQSLDPXNGOKMIEBFZCWVJAT"))
- (define refC (string->list "FVPJIAOYEDRZXWGCTKUQSBNMHL"))
- ;; letter must be a char. Alphabet is a list from a string (list of chars)
- ;; gets the index of a specific letter in a sequence of letters
- (define (get-idx-of letter alphabet)
- (define (iter count a)
- (if (equal? (car a) letter)
- count
- (iter (+ 1 count) (cdr a))))
- (iter 0 alphabet))
- ;; gets specific index in a sequence of letters
- (define (get idx alphabet)
- (define (iter count a)
- (if (= 0 count) (car a) (iter (- count 1) (cdr a))))
- (iter idx alphabet))
- (define (pass-through-rotor letter rotor)
- (get (get-idx-of letter alph) rotor))
- (define (reflect-through-rotor letter rotor)
- (get (get-idx-of letter rotor) alph))
- ;; rotating the rotor
- (define (rotate-rotor times rotor)
- ;; shift it all up one
- (define (iter times to-shift rotor )
- (if (= 0 times) (append rotor to-shift)
- (iter (- times 1) (append to-shift (list (car rotor))) (cdr rotor))))
- (iter times (list) rotor))
- (define (pass-letter letter rotor1 rotor2 rotor3 reflector)
- ;; letter passes through r1, r2, r3, hits reflector board and goes back
- ;; through r3, r2, r1
- (define in (pass-through-rotor
- (pass-through-rotor
- (pass-through-rotor letter rotor1)
- rotor2)
- rotor3))
- (reflect-through-rotor
- (reflect-through-rotor
- (reflect-through-rotor
- (reflect-through-rotor in reflector)
- rotor3)
- rotor2)
- rotor1))
- (define (get-next-rotor rotor rotor-prev notch)
- (if (equal? (car rotor-prev) notch) (rotate-rotor 1 rotor) rotor))
- (define (encrypt-text text rotor1 rotor2 rotor3 reflector plugs notches)
- (define notch1 (car notches))
- (define notch2 (cadr notches))
- (define notch3 (caddr notches))
- (define (iter text encrypted r1 r2 r3 count)
- (if (empty? text) (list->string encrypted)
- (iter (cdr text) (append encrypted (list
- (pass-letter (car text)
- r1 r2 r3 reflector)))
- (rotate-rotor 1 r1)
- (if (= 0 (modulo count 26)) (rotate-rotor 1 r2) r2)
- (if (= 0 (modulo count 676)) (rotate-rotor 1 r3) r3)
- (+ count 1))))
- (iter (string->list text) (list) rotor1 rotor2 rotor3 1))
- (provide get-idx-of get pass-through-rotor reflect-through-rotor
- rotate-rotor pass-letter encrypt-text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement