Advertisement
ksoltan

Engima1.0.rkt

Apr 27th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.85 KB | None | 0 0
  1. #lang racket
  2. (define alph (string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
  3. (define r1 (string->list "EKMFLGDQVZNTOWYHXUSPAIBRCJ"))
  4. (define r2 (string->list "AJDKSIRUXBLHWTMCQGZNPYFVOE"))
  5. (define r3 (string->list "BDFHJLCPRTXVZNYEIWGAKMUSQO"))
  6. (define r4 (string->list "ESOVPZJAYQUIRHXLNFTGKDCMWB"))
  7. (define r5 (string->list "VZBRGITYUPSDNHLXAWMJQOFECK"))
  8. (define refB (string->list "YRUHQSLDPXNGOKMIEBFZCWVJAT"))
  9. (define refC (string->list "FVPJIAOYEDRZXWGCTKUQSBNMHL"))
  10.  
  11. ;; letter must be a char. Alphabet is a list from a string (list of chars)
  12. ;; gets the index of a specific letter in a sequence of letters
  13. (define (get-idx-of letter alphabet)
  14.   (define (iter count a)
  15.     (if (equal? (car a) letter)
  16.         count
  17.         (iter (+ 1 count) (cdr a))))
  18.   (iter 0 alphabet))
  19.  
  20. ;; gets specific index in a sequence of letters
  21. (define (get idx alphabet)
  22.   (define (iter count a)
  23.     (if (= 0 count) (car a) (iter (- count 1) (cdr a))))
  24.   (iter idx alphabet))
  25.  
  26. (define (pass-through-rotor letter rotor)
  27.   (get (get-idx-of letter alph) rotor))
  28.  
  29. (define (reflect-through-rotor letter rotor)
  30.   (get (get-idx-of letter rotor) alph))
  31.  
  32. ;; rotating the rotor
  33. (define (rotate-rotor times rotor)
  34.   ;; shift it all up one
  35.   (define (iter times to-shift rotor )
  36.     (if (= 0 times) (append rotor to-shift)
  37.         (iter (- times 1) (append to-shift (list (car rotor))) (cdr rotor))))
  38.   (iter times (list) rotor))
  39.  
  40. (define (pass-letter letter rotor1 rotor2 rotor3 reflector)
  41.   ;; letter passes through r1, r2, r3, hits reflector board and goes back
  42.   ;; through r3, r2, r1
  43.   (define in (pass-through-rotor
  44.               (pass-through-rotor
  45.                (pass-through-rotor letter rotor1)
  46.                rotor2)
  47.               rotor3))
  48.   (reflect-through-rotor
  49.    (reflect-through-rotor
  50.     (reflect-through-rotor
  51.      (reflect-through-rotor in reflector)
  52.      rotor3)
  53.     rotor2)
  54.    rotor1))
  55.  
  56. (define (get-next-rotor rotor rotor-prev notch)
  57.   (if (equal? (car rotor-prev) notch) (rotate-rotor 1 rotor) rotor))
  58.  
  59. (define (encrypt-text text rotor1 rotor2 rotor3 reflector plugs notches)
  60.   (define notch1 (car notches))
  61.   (define notch2 (cadr notches))
  62.   (define notch3 (caddr notches))
  63.  
  64.   (define (iter text encrypted r1 r2 r3 count)
  65.     (if (empty? text) (list->string encrypted)
  66.         (iter (cdr text) (append encrypted (list
  67.                                             (pass-letter (car text)
  68.                                                              r1 r2 r3 reflector)))
  69.               (rotate-rotor 1 r1)
  70.               (if (= 0 (modulo count 26)) (rotate-rotor 1 r2) r2)
  71.               (if (= 0 (modulo count 676)) (rotate-rotor 1 r3) r3)
  72.               (+ count 1))))
  73.   (iter (string->list text) (list) rotor1 rotor2 rotor3 1))
  74.  
  75. (provide get-idx-of get pass-through-rotor reflect-through-rotor
  76.          rotate-rotor pass-letter encrypt-text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement