Advertisement
ksoltan

rotor.rkt

May 19th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 6.50 KB | None | 0 0
  1. #lang racket
  2. (require "accessors.rkt")
  3. (define alph (string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
  4.  
  5. (define rotor1 (string->list "EKMFLGDQVZNTOWYHXUSPAIBRCJ"))
  6. (define rotor2 (string->list "AJDKSIRUXBLHWTMCQGZNPYFVOE"))
  7. (define rotor3 (string->list "BDFHJLCPRTXVZNYEIWGAKMUSQO"))
  8. (define rotor4 (string->list "ESOVPZJAYQUIRHXLNFTGKDCMWB"))
  9. (define rotor5 (string->list "VZBRGITYUPSDNHLXAWMJQOFECK"))
  10. (define refB (string->list "YRUHQSLDPXNGOKMIEBFZCWVJAT"))
  11. (define refC (string->list "FVPJIAOYEDRZXWGCTKUQSBNMHL"))
  12.  
  13. (define rI (list alph 0 (string->list "Q") rotor1 alph))
  14. (define rII (list alph 0 (string->list "E") rotor2 alph))
  15. (define rIII (list alph 0 (string->list "V") rotor3 alph))
  16. (define rIV (list alph 0 (string->list "J") rotor4 alph))
  17. (define rV (list alph 0 (string->list "Z") rotor5 alph))
  18. (define rB (list alph 0 (list) refB alph))
  19. (define rC (list alph 0 (list) refC alph))
  20. (define a (list alph 0 (list) alph alph)) ;; alphabet as a rotor
  21.  
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. (define (get-rotor-alph r) (car r))
  25.  
  26. (define (get-times-rotated r) (cadr r))
  27.  
  28. (define (get-rotor-notch r) (caddr r))
  29.  
  30. (define (get-rotor-encrypt r) (cadddr r))
  31.  
  32. (define (get-rotor-a r) (car (cddddr r))) ;; get the ground alphabet
  33.  
  34. (define (set-rotor-a a r)
  35.   (list (get-rotor-alph r)
  36.         (get-times-rotated r)
  37.         (get-rotor-notch r)
  38.         (get-rotor-encrypt r)
  39.         a))
  40.  
  41. ;; when rotor rotates: the times increments by 1, the rotor rotates once
  42. (define (rotate r) (list (rotate-rotor 1 (get-rotor-alph r))
  43.                          (modulo (+ 1 (get-times-rotated r)) 26)
  44.                          (get-rotor-notch r)
  45.                          (get-rotor-encrypt r)
  46.                          (get-rotor-a r)))
  47.  
  48. (define (rotate-times r times) (list
  49.                                 (rotate-rotor times (get-rotor-alph r))
  50.                                 (modulo (+ times (get-times-rotated r)) 26)
  51.                                 (get-rotor-notch r)
  52.                                 (get-rotor-encrypt r)
  53.                                 (get-rotor-a r)))
  54.  
  55. ;; rotating the rotor
  56. (define (rotate-rotor times rotor)
  57.   ;; shift it all up one
  58.   (define (iter times to-shift r)
  59.     (if (= 0 times) (append r to-shift)
  60.         (iter (- times 1) (append to-shift (list (car r))) (cdr r))))
  61.   (iter times (list) rotor))
  62.  
  63. (define (set-rotor-ground-settings is r1 r2 r3)
  64.   (let* ((r11 (rotate-times r1 (get-idx-of (car is) (get-rotor-a r1))))
  65.          (r21 (rotate-times r2 (get-idx-of (cadr is) (get-rotor-a r2))))
  66.          (r31 (rotate-times r3 (get-idx-of (caddr is) (get-rotor-a r3)))))
  67.     (list r11 r21 r31)))
  68.  
  69. ;; letter passed in is a char. The plugs are lists of chars
  70. ;; returns a character
  71. (define (get-plug plugs letter)
  72.   (define (iter plugs-left)
  73.     (printf "Plugs-left: ~a\n" plugs-left)
  74.     (if (empty? plugs-left) letter
  75.         (let* ((plug (car plugs-left))
  76.                (first (car plug))
  77.                (second (cadr plug)))
  78.           (printf "letter: ~a, plug: ~a, first: ~a, second: ~a\n" letter plug first second)
  79.           (if (equal? first letter) second
  80.               (if (equal? second letter) first
  81.                   (iter (cdr plugs-left)))))))
  82.   (iter plugs))
  83.  
  84. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  85. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  86. ;; idx is from 1
  87. (define (get-idx-next-letter-forward r1 r2 idx-letter-r1)
  88.   (get-alph-idx
  89.    (modulo (+ (- idx-letter-r1
  90.                  (get-times-rotated r1))
  91.               26
  92.               (get-times-rotated r2))
  93.            26)))
  94.  
  95. ;; encryption after the reflector
  96. (define (get-idx-next-letter-backward r2 r1 idx-letter-r2)
  97.   (get-alph-idx (modulo (- (+
  98.               idx-letter-r2
  99.               (get-times-rotated r1))
  100.              (get-times-rotated r2))
  101.           26)))
  102.  
  103. ;; idx-prev from 1
  104. (define (pass-through-rotor-forward r2 idx-prev . rprev)
  105.   (define r1 (if (empty? rprev) a (car rprev)))
  106.   (define idx-going-in (- (get-idx-next-letter-forward r1 r2 idx-prev) 1))
  107.   (define letter-in-r2 (get idx-going-in (get-rotor-a r2)))
  108.   (define idx-in-alph (get-idx-of letter-in-r2 (get-rotor-a r2)))
  109.   (define encrypted-letter (get idx-in-alph (get-rotor-encrypt r2)))
  110.   (+ (get-idx-of encrypted-letter alph) 1))
  111.  
  112. ;; idx is from 1
  113. (define (pass-through-ref ref rprev idx)
  114.   (+ 1 (get-idx-of (get (- (get-idx-next-letter-forward rprev ref idx) 1) (get-rotor-encrypt ref)) alph)))
  115.  
  116. (define (pass-through-rotor-backward r1 idx-r2 . r2)
  117.   (let ((idx (- (get-idx-next-letter-backward (car r2) r1 idx-r2) 1)))
  118.     (+ 1 (get-idx-of (get
  119.                       (get-idx-of (get idx alph)
  120.                                   (get-rotor-encrypt r1))
  121.                       (get-rotor-alph r1))
  122.                 (get-rotor-alph r1)))))
  123.  
  124. (define (pass-through r1 r2 r3 ref char)
  125.   (define idx (+ (get-idx-of char alph) 1))
  126.   (define idx-to-reflector (pass-through-rotor-forward r3
  127.                                                        (pass-through-rotor-forward
  128.                                                         r2
  129.                                                         (pass-through-rotor-forward r1 idx)
  130.                                                         r1)
  131.                                                        r2))
  132.   (get (- (get-idx-next-letter-backward r1
  133.                                 a
  134.                                 (pass-through-rotor-backward
  135.                                  r1
  136.                                  (pass-through-rotor-backward
  137.                                   r2
  138.                                   (pass-through-rotor-backward
  139.                                    r3
  140.                                    (pass-through-ref ref r3 idx-to-reflector)
  141.                                    ref)
  142.                                   r3)
  143.                                  r2)) 1)
  144.        alph))
  145.  
  146. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  147. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  148. (provide rotate-rotor rotate-times get-rotor-encrypt get-rotor-notch get-rotor-alph
  149.          get-times-rotated rI rII rIII rIV rV rB rC a
  150.          set-rotor-ground-settings get-plug rotate
  151.          get-idx-next-letter-backward get-idx-next-letter-forward
  152.          pass-through-rotor-forward pass-through-ref pass-through-rotor-backward
  153.          pass-through rotor1 rotor2 rotor3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement