[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/6ca7l33n    [ raw code | output | fork ]

Ruby, pasted on Mar 2:
; <b style="color:#000;background:#ffff66">affine-shift</b> <b style="color:#000;background:#66ffff">cipher</b>

(define (inverse x n)
  (let loop ((x (modulo x n)) (a 1))
    (cond ((zero? x) (return #f))
          ((= x 1) a)
          (else (let ((q (- (quotient n x))))
                  (loop (+ n (* q x)) (modulo (* q a) n)))))))

(define (c->int c)
  (- (char->integer c) 65))

(define (int->c i)
  (integer->char (+ i 65)))

(define (<b style="color:#000;background:#66ffff">cipher</b> a b)
  (lambda (x)
    (modulo (+ (* a x) b) 26)))

(define (plain a b)
  (lambda (x)
    (modulo (* (inverse a 26) (- x b)) 26)))

(define (mapping func text)
  (list->string
    (map int->c
      (map func
        (map c->int
          (string->list text))))))

(define (encipher a b text)
  (mapping (<b style="color:#000;background:#66ffff">cipher</b> a b) text))

(define (decipher a b text)
  (mapping (plain a b) text))

(display (encipher 5 8 "PROGRAMMINGPRAXIS")) (newline)
(display (decipher 5 8 "FPAMPIQQWVMFPITWU")) (newline)


Output:
1
2
3
Line 1: syntax error, unexpected '<', expecting $end
; <b style="color:#000;background:#ffff66">affine-shift</b> <b style="color:#000;background:#66ffff">cipher</b>
   ^


Create a new paste based on this one


Comments: