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

VanessaRobl's solution

to Atbash Cipher in the PHP Track

Published at May 23 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 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.



require "atbash-cipher.php";

class AtbashTest extends PHPUnit\Framework\TestCase
    public function testEncodeNo()
        $this->assertEquals('ml', encode('no'));

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

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

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

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

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

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

    public function testEncodeAllTheThings()
        $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 $text) : string {
	$alphabet = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
	$result = "";
	$text = stripStringOfSpecialCharacters($text);
	//iterate through given text
	for($i = 0, $len = strlen($text); $i < $len; $i++) {
		//add a space after every 5 chars
		if($i % 5 == 0 && $i != 0) {
			$result .= " ";
		$result .= returnEncodedLetter($text[$i], $alphabet);
	return $result;

function returnEncodedLetter(string $letter, array $alphabet) : string {
	//if it's a number return the number unchanged
	if (ctype_digit($letter)) {
		return $letter;
	//look up index of current letter in alphabet array
	$alphabetIndex = array_search($letter, $alphabet);
	//from there, get the encoded respective letter index
	$encodedAlphabetIndex = 25 - $alphabetIndex;
	//return the letter at this index
	return $alphabet[$encodedAlphabetIndex];

function stripStringOfSpecialCharacters (string $text) : string {
	return preg_replace('/[^a-z0-9\-]/', '', strtolower($text));

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?