-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First passing test for the substituion sypher
- Loading branch information
Showing
11 changed files
with
360 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Feature: Encrypt Plain Text | ||
|
||
As a privacy nut I would like to encrypt text in the worst possible way by using a substitution cypher | ||
|
||
The cypher will replace each letter with another letter. Each letter will be used exactly once. | ||
|
||
My plain and text will be case insensitive and punctuation marks will not be encrypted | ||
|
||
Scenario: A simple plain text using all letters from the alphabet | ||
Given the plain text | ||
""" | ||
the quick brown fox jumps over the lazy dog | ||
""" | ||
When I encrypt the text using the following letter substitution | ||
| Plain text letter | Cypher text letter | | ||
| A | B | | ||
| B | C | | ||
| C | D | | ||
| D | E | | ||
| E | F | | ||
| F | G | | ||
| G | H | | ||
| H | I | | ||
| I | J | | ||
| J | K | | ||
| K | L | | ||
| L | M | | ||
| M | N | | ||
| N | O | | ||
| O | P | | ||
| P | Q | | ||
| Q | R | | ||
| R | S | | ||
| S | T | | ||
| T | U | | ||
| U | V | | ||
| V | W | | ||
| W | X | | ||
| X | Y | | ||
| Y | Z | | ||
| Z | A | | ||
Then the cypher text should be | ||
""" | ||
UIF RVJDL CSPXO GPY KVNQT PWFS UIF MBAZ EPH | ||
""" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using TechTalk.SpecFlow; | ||
|
||
namespace Cypher.Tests | ||
{ | ||
[Binding] | ||
public class EncryptPlainTextSteps | ||
{ | ||
private string _plainText; | ||
private string _encryptedText; | ||
|
||
[Given(@"the plain text")] | ||
public void GivenThePlainText(string plainText) | ||
{ | ||
_plainText = plainText; | ||
} | ||
|
||
[When(@"I encrypt the text using the following letter substitution")] | ||
public void WhenIEncryptTheTextUsingTheFollowingLetterSubstitution(Table table) | ||
{ | ||
var cypher = new SubstitutionCypher(); | ||
|
||
foreach (var tableRow in table.Rows) | ||
{ | ||
cypher.Substitute(tableRow[0]).For(tableRow[1]); | ||
} | ||
|
||
_encryptedText = cypher.Encrypt(_plainText); | ||
} | ||
|
||
[Then(@"the cypher text should be")] | ||
public void ThenTheCypherTextShouldBe(string expectedCypherText) | ||
{ | ||
_encryptedText.Should().Be(expectedCypherText); | ||
} | ||
} | ||
|
||
public class SubstitutionCypher | ||
{ | ||
private readonly List<Substitution> _substitutionList; | ||
|
||
public SubstitutionCypher() | ||
{ | ||
_substitutionList = new List<Substitution>(); | ||
} | ||
|
||
public Substitution Substitute(string letterToSubstitute) | ||
{ | ||
var substitution = new Substitution(letterToSubstitute.ToUpper()); | ||
|
||
_substitutionList.Add(substitution); | ||
|
||
return substitution; | ||
} | ||
|
||
public string Encrypt(string plainText) | ||
{ | ||
var encryptedText = ""; | ||
foreach (var letter in plainText.ToUpper()) | ||
{ | ||
var substitution = _substitutionList.SingleOrDefault( | ||
s => s.LetterToSubstitute.Equals(letter.ToString(), StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (substitution != null) | ||
{ | ||
encryptedText += substitution.LetterToSubstituteWith; | ||
} | ||
else | ||
{ | ||
encryptedText += letter; | ||
} | ||
} | ||
|
||
return encryptedText; | ||
} | ||
} | ||
|
||
public static class SubstitutionExtensionMethods | ||
{ | ||
public static void For(this Substitution substitution, string letterToSubstituteWith) | ||
{ | ||
substitution.LetterToSubstituteWith = letterToSubstituteWith.ToUpper(); | ||
} | ||
} | ||
|
||
public class Substitution | ||
{ | ||
public string LetterToSubstitute { get; } | ||
public string LetterToSubstituteWith { get; set; } | ||
|
||
public Substitution(string letterToSubstitute) | ||
{ | ||
LetterToSubstitute = letterToSubstitute; | ||
} | ||
} | ||
} |
Oops, something went wrong.