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

GHDIShfdiod876dhs's solution

to Atbash Cipher in the PHP Track

Published at Jul 03 2019 · 0 comments
Instructions
Test suite
Solution

Note:

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.

Examples

  • 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
    

Source

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.

atbash-cipher_test.php

<?php

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));
    }
}
<?php

function encode(string $str): string
{
  $alphabet = 'abcdefghijklmnopqrstuvwxyz';
  $result = '';
  preg_match_all('/[a-z0-9]/', strtolower($str), $matches);

  foreach($matches[0] as $char) {
    $idx = strpos($alphabet, $char);
    if ($idx === false) $result .= $char;
    else $result .= $alphabet[25 - $idx];
  }

  return trim(preg_replace('/(.{5})/', '$1 ', $result));
}

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?