Advertisement
ksoltan

enigma3.0,rkt

May 19th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.87 KB | None | 0 0
  1. #lang racket
  2. (require "accessors.rkt")
  3. (require "rotor.rkt")
  4. ;; a rotor will be a list of 1) the settings 2) the numbers of times
  5. ;; it has rotated % 26 3) it's notch 4) the corresponding encryption
  6.  
  7. ;; the letter passed to the next rotor will not fall onto the same position
  8. ;; it depends on the number of times this rotor rotated and the next rotor
  9. ;; rotated; encryption before the reflector
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11. (define alph (string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
  12.  
  13. ;; plugs are passed in as a list of strings and converted to a list of string->list
  14. ;; pairs of letters: (list (string->list "AD") (string->list "BC"))
  15. (define (encrypt r1 r2 r3 ref string pl init-sets)
  16.   (define plugs (convert-to-list-of-chars pl))
  17.   (define str (string->list string))
  18.   (define is (string->list init-sets))
  19.   (define (iter r1 r2 r3 ref string encoded)
  20.     ;; first must rotate all the rotors
  21.     (if (empty? string) (list->string encoded)
  22.         (if (= -1 (get-idx-of (car string) alph))
  23.             (iter r1 r2 r3 ref (cdr string) (append encoded
  24.                                                     (string->list " ")))
  25.             (let* ((r1first (car (get-rotor-alph r1))) ;; first letter in the first rotor before rotation
  26.                (r2first (car (get-rotor-alph r2))) ;; first letter of second rotor before rotation
  27.                (r11 (rotate-times r1 1)) ;; the first rotor is always rotated
  28.                (r21 (if (or (equal? r1first (car (get-rotor-notch r1)))
  29.                             (equal? r2first (car (get-rotor-notch r2)))) ;; second rotor rotated if first rotor passes the notch
  30.                         ;; or the second rotor is about to pass its notch (if these two happen simultaneously, rotate only once)
  31.                         (rotate-times r2 1) r2))
  32.                (r31 (if (equal? r2first (car (get-rotor-notch r2)))
  33.                         (rotate-times r3 1) r3))) ;; rotate the third rotor is the second rotor passes its notch
  34.               (printf "r1-first: ~a, r2-first: ~a, r1-notch: ~a, r2-notch: ~a\n" r1first r2first
  35.                       (get-rotor-notch r1) (get-rotor-notch r2))
  36.               (printf "Current settings: ~a\n~a\n~a\n\n" (get-rotor-alph r11) (get-rotor-alph r21) (get-rotor-alph r31))
  37.               (iter r11 r21 r31 ref (cdr string)
  38.                      (append encoded (list
  39.                               (get-plug plugs (pass-through
  40.                                                r11 r21 r31 ref
  41.                                                (get-plug plugs
  42.                                                          (car string)))))))))))
  43.   (define rotors (set-rotor-ground-settings is r1 r2 r3))
  44.   (printf "Initial settings: ~a\n~a\n~a\n\n" (car rotors) (cadr rotors) (caddr rotors))
  45.   (iter (car rotors) (cadr rotors) (caddr rotors) ref str (list)))
  46.  
  47. (provide alph encrypt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement