🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # pldupont's solution

## to Simple Cipher in the C# Track

Published at Feb 16 2021 · 0 comments
Instructions
Test suite
Solution

Implement a simple shift cipher like Caesar and a more secure substitution cipher.

## Step 1

"If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out. If anyone wishes to decipher these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D, for A, and so with the others." —Suetonius, Life of Julius Caesar

Ciphers are very straight-forward algorithms that allow us to render text less readable while still allowing easy deciphering. They are vulnerable to many forms of cryptanalysis, but we are lucky that generally our little sisters are not cryptanalysts.

The Caesar Cipher was used for some messages from Julius Caesar that were sent afield. Now Caesar knew that the cipher wasn't very good, but he had one ally in that respect: almost nobody could read well. So even being a couple letters off was sufficient so that people couldn't recognize the few words that they did know.

Your task is to create a simple shift cipher like the Caesar Cipher. This image is a great example of the Caesar Cipher: For example:

Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu". Obscure enough to keep our message secret in transit.

When "ldpdsdqgdehdu" is put into the decode function it would return the original "iamapandabear" letting your friend read your original message.

## Step 2

Shift ciphers are no fun though when your kid sister figures it out. Try amending the code to allow us to specify a key and use that for the shift distance. This is called a substitution cipher.

Here's an example:

Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear" would return the original "iamapandabear".

Given the key "ddddddddddddddddd", encoding our string "iamapandabear" would return the obscured "ldpdsdqgdehdu"

In the example above, we've set a = 0 for the key value. So when the plaintext is added to the key, we end up with the same message coming out. So "aaaa" is not an ideal key. But if we set the key to "dddd", we would get the same thing as the Caesar Cipher.

## Step 3

The weakest link in any cipher is the human being. Let's make your substitution cipher a little more fault tolerant by providing a source of randomness and ensuring that the key contains only lowercase letters.

If someone doesn't submit a key at all, generate a truly random key of at least 100 alphanumeric characters in length.

## Extensions

Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis. Substitution ciphers help that, but are still very vulnerable when the key is short or if spaces are preserved. Later on you'll see one solution to this problem in the exercise "crypto-square".

If you want to go farther in this field, the questions begin to be about how we can exchange keys in a secure way. Take a look at Diffie-Hellman on Wikipedia for one of the first implementations of this scheme.

## 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 SimpleCipher.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

Substitution Cipher at Wikipedia http://en.wikipedia.org/wiki/Substitution_cipher

### SimpleCipherTests.cs

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

using System;
using Xunit;

public class SimpleCipherTests
{
[Fact]
public void Random_key_cipher_can_encode()
{
var sut = new SimpleCipher();
Assert.Equal(sut.Key.Substring(0, 10), sut.Encode("aaaaaaaaaa"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Random_key_cipher_can_decode()
{
var sut = new SimpleCipher();
Assert.Equal("aaaaaaaaaa", sut.Decode(sut.Key.Substring(0, 10)));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Random_key_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
{
var sut = new SimpleCipher();
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Random_key_cipher_key_is_made_only_of_lowercase_letters()
{
var sut = new SimpleCipher();
Assert.Matches("^[a-z]+\$", sut.Key);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_can_encode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("abcdefghij", sut.Encode("aaaaaaaaaa"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_can_decode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("aaaaaaaaaa", sut.Decode("abcdefghij"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_can_double_shift_encode()
{
var sut = new SimpleCipher("iamapandabear");
Assert.Equal("qayaeaagaciai", sut.Encode("iamapandabear"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_can_wrap_on_encode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("zabcdefghi", sut.Encode("zzzzzzzzzz"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_can_wrap_on_decode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("zzzzzzzzzz", sut.Decode("zabcdefghi"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_can_encode_messages_longer_than_the_key()
{
var sut = new SimpleCipher("abc");
Assert.Equal("iboaqcnecbfcr", sut.Encode("iamapandabear"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_can_decode_messages_longer_than_the_key()
{
var sut = new SimpleCipher("abc");
Assert.Equal("iamapandabear", sut.Decode("iboaqcnecbfcr"));
}
}``````

### SimpleCipher.cs

``````using System;
using System.Linq;

public class SimpleCipher
{
private string _baseKey;
private string _key = "";
private Random _random = new Random();

public SimpleCipher()
{
_baseKey = RandomString(100);
InitKey(10);
}

public SimpleCipher(string key)
{
if (key == null || key.Length == 0)
{
_baseKey = RandomString(100);
}
else
{
_baseKey = key;
}
InitKey(10);
}

public string Key
{
get
{
return _key;
}
}

public string RandomString(int length)
{
const string chars = "abcdefghijklmnopqrstuvwxyz";
return new string(Enumerable.Repeat(chars, length).Select(s => s[_random.Next(s.Length)]).ToArray());
}
public string Encode(string plaintext)
{
InitKey(plaintext.Length);
string ciphertext = "";
for (int i = 0; i < plaintext.Length; i++)
{
var v = (int) plaintext[i] + ((int) _key[i] - (int) 'a');
if (v > (int) 'z') {
v -= 26;
}
ciphertext += (char) v;
}
return ciphertext;
}

public string Decode(string ciphertext)
{
InitKey(ciphertext.Length);
string plaintext = "";
for (int i = 0; i < ciphertext.Length; i++)
{
var v = (int) ciphertext[i] - ((int) _key[i] - (int) 'a');
if (v < (int) 'a')
{
v += 26;
}
plaintext += (char) v;
}
return plaintext;
}

private void InitKey(int length) {
while (_key.Length < length)
{
_key += _baseKey;
}
}
}``````

### SimpleCipherTests.cs

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

using System;
using Xunit;

public class SimpleCipherTests
{
[Fact]
public void Random_key_cipher_can_encode()
{
var sut = new SimpleCipher();
Assert.Equal(sut.Key.Substring(0, 10), sut.Encode("aaaaaaaaaa"));
}

[Fact]
public void Random_key_cipher_can_decode()
{
var sut = new SimpleCipher();
Assert.Equal("aaaaaaaaaa", sut.Decode(sut.Key.Substring(0, 10)));
}

[Fact]
public void Random_key_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
{
var sut = new SimpleCipher();
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
}

[Fact]
public void Random_key_cipher_key_is_made_only_of_lowercase_letters()
{
var sut = new SimpleCipher();
Assert.Matches("^[a-z]+\$", sut.Key);
}

[Fact]
public void Substitution_cipher_can_encode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("abcdefghij", sut.Encode("aaaaaaaaaa"));
}

[Fact]
public void Substitution_cipher_can_decode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("aaaaaaaaaa", sut.Decode("abcdefghij"));
}

[Fact]
public void Substitution_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
}

[Fact]
public void Substitution_cipher_can_double_shift_encode()
{
var sut = new SimpleCipher("iamapandabear");
Assert.Equal("qayaeaagaciai", sut.Encode("iamapandabear"));
}

[Fact]
public void Substitution_cipher_can_wrap_on_encode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("zabcdefghi", sut.Encode("zzzzzzzzzz"));
}

[Fact]
public void Substitution_cipher_can_wrap_on_decode()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("zzzzzzzzzz", sut.Decode("zabcdefghi"));
}

[Fact]
public void Substitution_cipher_can_encode_messages_longer_than_the_key()
{
var sut = new SimpleCipher("abc");
Assert.Equal("iboaqcnecbfcr", sut.Encode("iamapandabear"));
}

[Fact]
public void Substitution_cipher_can_decode_messages_longer_than_the_key()
{
var sut = new SimpleCipher("abc");
Assert.Equal("iamapandabear", sut.Decode("iboaqcnecbfcr"));
}

[Fact]
public void Two_cipher_with_empty_string_key_generate_different_keys()
{
var sut1 = new SimpleCipher("");
var sut2 = new SimpleCipher("");
Assert.NotEqual(sut1.Key, sut2.Key);
}

[Fact]
public void Two_cipher_with_null_string_key_generate_different_keys()
{
var sut1 = new SimpleCipher(null);
var sut2 = new SimpleCipher(null);
Assert.NotEqual(sut1.Key, sut2.Key);
}

// [Fact]
// public void Two_cipher_with_null_string_key_generate_different_keys()
// {
//     var sut1 = new SimpleCipher(null);
//     var sut2 = new SimpleCipher(null);
//     Assert.NotEqual(sut1.Key, sut2.Key);
// }

}``````

## 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?