Avatar of MatsLanGoH

MatsLanGoH's solution

to Atbash Cipher in the Elm Track

Published at Jul 09 2019 · 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

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import AtbashCipher exposing (decode, encode)
import Expect
import Test exposing (..)


tests : Test
tests =
    describe "AtbashCipher"
        [ test "encode no" <|
            \() -> Expect.equal "ml" (encode "no")
        , skip <|
            test "encode yes" <|
                \() -> Expect.equal "bvh" (encode "yes")
        , skip <|
            test "encode OMG" <|
                \() -> Expect.equal "lnt" (encode "OMG")
        , skip <|
            test "encode O M G" <|
                \() -> Expect.equal "lnt" (encode "O M G")
        , skip <|
            test "encode long word" <|
                \() -> Expect.equal "nrmwy oldrm tob" (encode "mindblowingly")
        , skip <|
            test "encode numbers" <|
                \() -> Expect.equal "gvhgr mt123 gvhgr mt" (encode "Testing, 1 2 3, testing.")
        , skip <|
            test "encode sentence" <|
                \() -> Expect.equal "gifgs rhurx grlm" (encode "Truth is fiction.")
        , skip <|
            test "encode all things" <|
                \() ->
                    Expect.equal "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
                        (encode "The quick brown fox jumps over the lazy dog.")
        , skip <|
            test "decode word" <|
                \() -> Expect.equal "exercism" (decode "vcvix rhn")
        , skip <|
            test "decode sentence" <|
                \() ->
                    Expect.equal "anobstacleisoftenasteppingstone"
                        (decode "zmlyh gzxov rhlug vmzhg vkkrm thglm v")
        ]
module AtbashCipher exposing (decode, encode)


encode : String -> String
encode plain =
    plain
        |> String.filter Char.isAlphaNum
        |> String.toLower
        |> String.map transposeCipher
        |> insertSpaceAfter5


decode : String -> String
decode cipher =
    cipher
        |> String.filter Char.isAlphaNum
        |> String.map transposeCipher


transposeCipher : Char -> Char
transposeCipher char =
    -- NOTE: Maybe using types instead of hardcoded ascii values might be safer?
    let
        code =
            Char.toCode char
    in
    if code < 97 then
        char

    else if code > 122 then
        char

    else
        Char.fromCode (97 + 122 - code)


insertSpaceAfter5 : String -> String
insertSpaceAfter5 string =
    insertSpaces 5 string


insertSpaces : Int -> String -> String
insertSpaces int string =
    if String.length string > int then
        String.left int string
            ++ " "
            ++ insertSpaces int (String.dropLeft int string)

    else
        string

Community comments

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

MatsLanGoH's Reflection

Took me an embarrassingly long time to figure out the Atbash algorithm, had to look it up on Wikipedia.

Notes:

- `transposeCipher` could be rewritten to use a custom Type (`Ascii`).
- `insertSpaces` could also be rewritten to be more readable.