Skip to content

Commit

Permalink
add atbash
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 committed Aug 4, 2018
1 parent 01ee210 commit 08cfb7e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
34 changes: 34 additions & 0 deletions atbash-cipher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Atbash Cipher

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
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`
## Source

Wikipedia [http://en.wikipedia.org/wiki/Atbash](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.
42 changes: 42 additions & 0 deletions atbash-cipher/atbash-cipher-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
;;; atbash-cipher-test.el --- Tests for Atbash Cipher (exercism)

;;; Commentary:

;;; Code:

(require 'cl)

(load-file "atbash-cipher.el")

(ert-deftest encode-no ()
(should (equal "ml" (encode "no"))))

(ert-deftest encode-yes ()
(should (equal "bvh" (encode "yes"))))

(ert-deftest encode-OMG ()
(should (equal "lnt" (encode "OMG"))))

(ert-deftest encode-O-M-G ()
(should (equal "lnt" (encode "O M G"))))

(ert-deftest encode-long-word ()
(should (equal "nrmwy oldrm tob"
(encode "mindblowingly"))))

(ert-deftest encode-numbers ()
(should (equal "gvhgr mt123 gvhgr mt"
(encode "Testing, 1 2 3, testing."))))

(ert-deftest encode-sentence ()
(should (equal "gifgs rhurx grlm"
(encode "Truth is fiction."))))

(ert-deftest encode-all-the-things ()
(let ((plaintext "The quick brown fox jumps over the lazy dog.")
(ciphertext "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"))
(should (equal ciphertext
(encode plaintext)))))

(provide 'atbash-cipher-test)
;;; atbash-cipher-test.el ends here
36 changes: 36 additions & 0 deletions atbash-cipher/atbash-cipher.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
;;; atbash-cipher.el --- Atbash-Cipher (exercism)

;;; Commentary:




;;; Code:

(require 'subr-x)
(defconst alphabet "abcdefghijklmnopqrstuvwxyz")
(defconst atbash
(let ((idx 0)
(reverse-alph-list (reverse alphabet)))
(mapcar #'(lambda (arg)
(let* ((reverse-char (substring reverse-alph-list idx (1+ idx))))
(incf idx)
(list arg (string-to-char reverse-char)))) alphabet)))

(defun encode (plaintext)
"Encode PLAINTEXT to atbash-cipher encoding."
(let* ((prepared-string (downcase (replace-regexp-in-string "[^[:alnum:]_-]" "" plaintext)))
(encoded-string (mapconcat #'(lambda(arg)
(char-to-string (or (car (alist-get arg atbash)) arg))) prepared-string ""))
(result "")
(counter 0))
(while (not (= counter (length encoded-string)))
(if (= 0 (% (1+ counter) 5))
(setq result (concat result (substring encoded-string counter (1+ counter)) " "))
(setq result (concat result (substring encoded-string counter (1+ counter)))))
(setq counter (1+ counter)))
(string-trim result)))


(provide 'atbash-cipher)
;;; atbash-cipher.el ends here

0 comments on commit 08cfb7e

Please sign in to comment.