Avatar of Rybak

Rybak's solution

to Atbash Cipher in the C# Track

Published at Jul 16 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

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit AtbashCipher.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

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

AtbashCipherTest.cs

// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class AtbashCipherTest
{
    [Fact]
    public void Encode_yes()
    {
        Assert.Equal("bvh", AtbashCipher.Encode("yes"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Encode_no()
    {
        Assert.Equal("ml", AtbashCipher.Encode("no"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Encode_omg()
    {
        Assert.Equal("lnt", AtbashCipher.Encode("OMG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Encode_spaces()
    {
        Assert.Equal("lnt", AtbashCipher.Encode("O M G"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Encode_mindblowingly()
    {
        Assert.Equal("nrmwy oldrm tob", AtbashCipher.Encode("mindblowingly"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Encode_numbers()
    {
        Assert.Equal("gvhgr mt123 gvhgr mt", AtbashCipher.Encode("Testing,1 2 3, testing."));
    }

    [Fact(Skip = "Remove to run test")]
    public void Encode_deep_thought()
    {
        Assert.Equal("gifgs rhurx grlm", AtbashCipher.Encode("Truth is fiction."));
    }

    [Fact(Skip = "Remove to run test")]
    public void Encode_all_the_letters()
    {
        Assert.Equal("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt", AtbashCipher.Encode("The quick brown fox jumps over the lazy dog."));
    }

    [Fact(Skip = "Remove to run test")]
    public void Decode_exercism()
    {
        Assert.Equal("exercism", AtbashCipher.Decode("vcvix rhn"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Decode_a_sentence()
    {
        Assert.Equal("anobstacleisoftenasteppingstone", AtbashCipher.Decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Decode_numbers()
    {
        Assert.Equal("testing123testing", AtbashCipher.Decode("gvhgr mt123 gvhgr mt"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Decode_all_the_letters()
    {
        Assert.Equal("thequickbrownfoxjumpsoverthelazydog", AtbashCipher.Decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Decode_with_too_many_spaces()
    {
        Assert.Equal("exercism", AtbashCipher.Decode("vc vix    r hn"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Decode_with_no_spaces()
    {
        Assert.Equal("anobstacleisoftenasteppingstone", AtbashCipher.Decode("zmlyhgzxovrhlugvmzhgvkkrmthglmv"));
    }
}
using System;
using System.Linq;

public static class AtbashCipher
{
    public static string Encode(string input) =>
        input.ToLower().AsParallel().Where(c => char.IsLetterOrDigit(c))
        .Select(c => char.IsLetter(c) ? (char)('m' * 2 - c + 1) : c)
        .Select((c, ind) => (ind + 1) % 5 == 0 & ind < input.Where(c2 => char.IsLetterOrDigit(c2)).Count() - 1 ? c.ToString() + ' ' : c.ToString())
        .Aggregate("", (st, i) => st + i);

    public static string Decode(string input) =>
        new string(input.Where(c => char.IsLetterOrDigit(c))
        .Select(c => char.IsLetter(c) ? (char)('m' * 2 - c + 1) : c).ToArray());
        
}

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?