Avatar of paulfioravanti

paulfioravanti's solution

to Atbash Cipher in the Elixir Track

Published at Aug 02 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

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.


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

Running tests

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.


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.


defmodule AtbashTest do
  use ExUnit.Case

  # @tag :pending
  test "encode no" do
    assert Atbash.encode("no") == "ml"

  @tag :pending
  test "encode yes" do
    assert Atbash.encode("yes") == "bvh"

  @tag :pending
  test "encode OMG" do
    assert Atbash.encode("OMG") == "lnt"

  @tag :pending
  test "encode O M G" do
    assert Atbash.encode("O M G") == "lnt"

  @tag :pending
  test "encode long word" do
    assert Atbash.encode("mindblowingly") == "nrmwy oldrm tob"

  @tag :pending
  test "encode numbers" do
    assert Atbash.encode("Testing, 1 2 3, testing.") == "gvhgr mt123 gvhgr mt"

  @tag :pending
  test "encode sentence" do
    assert Atbash.encode("Truth is fiction.") == "gifgs rhurx grlm"

  @tag :pending
  test "encode all the things" do
    plaintext = "The quick brown fox jumps over the lazy dog."
    cipher = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
    assert Atbash.encode(plaintext) == cipher

  @tag :pending
  test "decode exercism" do
    cipher = "vcvix rhn"
    plaintext = "exercism"
    assert Atbash.decode(cipher) == plaintext

  @tag :pending
  test "decode a sentence" do
    cipher = "zmlyh gzxov rhlug vmzhg vkkrm thglm v"
    plaintext = "anobstacleisoftenasteppingstone"
    assert Atbash.decode(cipher) == plaintext

  @tag :pending
  test "decode numbers" do
    cipher = "gvhgr mt123 gvhgr mt"
    plaintext = "testing123testing"
    assert Atbash.decode(cipher) == plaintext

  @tag :pending
  test "decode all the letters" do
    cipher = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
    plaintext = "thequickbrownfoxjumpsoverthelazydog"
    assert Atbash.decode(cipher) == plaintext


ExUnit.configure(exclude: :pending, trace: true)
defmodule Atbash do
  @alphabet_bounds ?a + ?z
  @group_size 5

  defguardp is_alpha?(char) when char in ?a..?z
  defguardp is_numeric?(char) when char in ?0..?9

  @doc """
  Encode a given plaintext to the corresponding ciphertext

  ## Examples

  iex> Atbash.encode("completely insecure")
  "xlnko vgvob rmhvx fiv"
  @spec encode(String.t()) :: String.t()
  def encode(plaintext) do
    |> String.downcase()
    |> to_transposed_charlist()
    |> Enum.chunk_every(@group_size)
    |> Enum.join(" ")

  @spec decode(String.t()) :: String.t()
  def decode(cipher) do
    |> to_transposed_charlist()
    |> List.to_string()

  defp to_transposed_charlist(string) do
    |> String.to_charlist()
    |> List.foldr([], &transpose/2)

  defp transpose(char, acc) when is_alpha?(char) do
    [@alphabet_bounds - char | acc]

  defp transpose(char, acc) when is_numeric?(char), do: [char | acc]
  defp transpose(_char, acc), do: acc

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?