🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of zembrowski

zembrowski's solution

to Atbash Cipher in the PHP Track

Published at Mar 17 2021 · 0 comments
Test suite

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 the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
  3. Execute the tests:

     % ./phpunit.phar atbash-cipher/atbash-cipher_test.php


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.



class AtbashTest extends PHPUnit\Framework\TestCase
    public static function setUpBeforeClass() : void
        require_once 'atbash-cipher.php';

    public function testEncodeNo() : void
        $this->assertEquals('ml', encode('no'));

    public function testEncodeYes() : void
        $this->assertEquals('bvh', encode('yes'));

    public function testEncodeOmg() : void
        $this->assertEquals('lnt', encode('OMG'));

    public function testEncodeOmgWithSpaces() : void
        $this->assertEquals('lnt', encode('O M G'));

    public function testEncodeLongWord() : void
        $this->assertEquals('nrmwy oldrm tob', encode('mindblowingly'));

    public function testEncodeNumbers() : void
        $this->assertEquals('gvhgr mt123 gvhgr mt', encode('Testing, 1 2 3, testing.'));

    public function testEncodeSentence() : void
        $this->assertEquals('gifgs rhurx grlm', encode('Truth is fiction.'));

    public function testEncodeAllTheThings() : void
        $plaintext = 'The quick brown fox jumps over the lazy dog.';
        $encoded = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt';
        $this->assertEquals($encoded, encode($plaintext));

function encode(string $input): string
    $alphabet = implode('', range('a', 'z'));
    $ciphers = strrev($alphabet);

    // filter out non-alphanumeric characters
    $input = preg_replace('/\W/', '', strtolower($input));

    $translation = strtr($input, $alphabet, $ciphers);

    return wordwrap($translation, 5, ' ', true);

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?