autokey.scm 923 B

1234567891011121314151617181920212223242526272829303132
  1. ;;;
  2. ;;; Autokey encipher and decipher
  3. ;;;
  4. ;;; Copyright 2016 Jason K. MacDuffie
  5. ;;; License: GPLv3+
  6. ;;;
  7. (define (autokey-encipher pt-in key-in)
  8. ;; Simply append the key to the plaintext, and then apply the
  9. ;; tabula recta
  10. (define k (append key-in pt-in))
  11. (runkey-encipher pt-in k))
  12. (define (autokey-decipher ct-in key-in)
  13. ;; This is a bit more involved. As the plaintext is generated,
  14. ;; we must add it to the key. This lends itself well to a
  15. ;; queue, which you would probably need to simulate anyway
  16. ;; if you used something like a mutable vector.
  17. (define k (queue))
  18. (for-each (lambda (i) (queue-add! k i)) key-in)
  19. (let loop ((ct ct-in)
  20. (pt '()))
  21. (if (null? ct)
  22. (reverse pt)
  23. (let ((key-next (letter- (car ct)
  24. (queue-remove! k))))
  25. (queue-add! k key-next)
  26. (loop (cdr ct)
  27. (cons key-next pt))))))