Avatar of cpaulbond

cpaulbond's solution

to Atbash Cipher in the Common Lisp Track

Published at Feb 29 2020 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.

Examples

  • Encoding test gives gvhg
  • Decoding gvhg gives test
  • Decoding gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt gives thequickbrownfoxjumpsoverthelazydog

Setup

Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.

Formatting

While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.

VIM

Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.

Emacs

Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq-default indent-tabs-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.

Source

Wikipedia http://en.wikipedia.org/wiki/Atbash

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

atbash-cipher-test.lisp

(ql:quickload "lisp-unit")
#-xlisp-test (load "atbash-cipher")

(defpackage #:atbash-cipher-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:atbash-cipher-test)

(define-test encode-no
  (assert-equal "ml" (atbash-cipher:encode "no")))

(define-test encode-yes
  (assert-equal "bvh" (atbash-cipher:encode "yes")))

(define-test encode-OMG
  (assert-equal "lnt" (atbash-cipher:encode "OMG")))

(define-test encode-O-M-G
  (assert-equal "lnt" (atbash-cipher:encode "O M G")))

(define-test encode-long-word
  (assert-equal "nrmwy oldrm tob"
                (atbash-cipher:encode "mindblowingly")))

(define-test encode-numbers
  (assert-equal "gvhgr mt123 gvhgr mt"
                (atbash-cipher:encode "Testing, 1 2 3, testing.")))

(define-test encode-sentence
  (assert-equal "gifgs rhurx grlm"
                (atbash-cipher:encode "Truth is fiction.")))

(define-test encode-all-the-things
  (let ((plaintext "The quick brown fox jumps over the lazy dog.")
        (cipher "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"))
    (assert-equal cipher (atbash-cipher:encode plaintext))))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :atbash-cipher-test))
(defpackage #:atbash-cipher
  (:use #:common-lisp)
  (:export #:encode))

(in-package #:atbash-cipher)


(defun group5 (lst)
  (loop for i in lst
     for n = 0 then (1+ n)
     when (and (not (zerop n)) (= 0 (rem n 5))) collect #\space
     collect i))


(defun encode-char (v)
  (if (alpha-char-p v)
      (code-char (- (char-code #\z) (- (char-code v) (char-code #\a))))
      v))


(defun encode (plaintext)
  (concatenate 'string
               (group5
                (map 'list #'encode-char
                     (map 'list #'char-downcase
                          (remove-if-not #'alphanumericp plaintext))))))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?