ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# freddie2025's solution

## to Atbash Cipher in the C# Track

Published at Dec 28 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

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;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public static class AtbashCipher
{
private static Dictionary<char,char> cipher = new Dictionary<char, char>()
{
{'a','z'}, {'b','y'}, {'c','x'}, {'d','w'}, {'e','v'}, {'f','u'},
{'g','t'}, {'h','s'}, {'i','r'}, {'j','q'}, {'k','p'}, {'l','o'},
{'m','n'}, {'n','m'}, {'o','l'}, {'p','k'}, {'q','j'}, {'r','i'},
{'s','h'}, {'t','g'}, {'u','f'}, {'v','e'}, {'w','d'}, {'x','c'},
{'y','b'}, {'z','a'}, {'1','1'}, {'2','2'}, {'3','3'}, {'4','4'},
{'5','5'}, {'6','6'}, {'7','7'}, {'8','8'}, {'9','9'}, {'0','0'}
};

public static string Encode(string plainValue)
{
return Regex.Replace(plainValue.ToLower().ToCharArray().Where(x => Char.IsLetterOrDigit(x)).Aggregate(new StringBuilder(), (x,y) => x.Append(cipher[y])).ToString(), ".{5}", "\$0 ").Trim();
}

public static string Decode(string encodedValue)
{
return encodedValue.ToLower().ToCharArray().Where(x => Char.IsLetterOrDigit(x)).Aggregate(new StringBuilder(), (x,y) => x.Append(cipher[y])).ToString().Trim();
}
}``````