Skip to content

Commit 8fec042

Browse files
authored
Add Autokey encoder (#509)
1 parent 329fc50 commit 8fec042

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using Algorithms.Encoders;
3+
using NUnit.Framework;
4+
using NUnit.Framework.Internal;
5+
6+
namespace Algorithms.Tests.Encoders
7+
{
8+
public static class AutokeyEncoderTests
9+
{
10+
[Test]
11+
public static void DecodedStringIsTheSame()
12+
{
13+
// Arrange
14+
var plainText = "PLAINTEXT";
15+
var keyword = "KEYWORD";
16+
var encoder = new AutokeyEncorder();
17+
18+
// Act
19+
var encoded = encoder.Encode(plainText, keyword);
20+
var decoded = encoder.Decode(encoded, keyword);
21+
22+
// Assert
23+
Assert.That(decoded, Is.EqualTo(plainText));
24+
}
25+
}
26+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Text;
4+
using System.Text.RegularExpressions;
5+
6+
namespace Algorithms.Encoders
7+
{
8+
/// <summary>
9+
/// Class for AutoKey encoding strings.
10+
/// </summary>
11+
public class AutokeyEncorder
12+
{
13+
/// <summary>
14+
/// Autokey Cipher is a type of polyalphabetic cipher.
15+
/// This works by choosing a key (a word or short phrase),
16+
/// then you append the plaintext to itself to form a longer key.
17+
/// </summary>
18+
/// <param name="plainText">The string to be appended to the key.</param>
19+
/// <param name="keyword">The string to be appended to the plaintext.</param>
20+
/// <returns>The Autokey encoded string (All Uppercase).</returns>
21+
public string Encode(string plainText, string keyword)
22+
{
23+
plainText = Regex.Replace(plainText.ToUpper(CultureInfo.InvariantCulture), "[^A-Z]", string.Empty);
24+
keyword = keyword.ToUpper(CultureInfo.InvariantCulture);
25+
26+
keyword += plainText;
27+
28+
StringBuilder cipherText = new StringBuilder();
29+
30+
for(int i = 0; i < plainText.Length; i++)
31+
{
32+
char plainCharacter = plainText[i];
33+
char keyCharacter = keyword[i];
34+
35+
int encryptedCharacter = (plainCharacter - 'A' + keyCharacter - 'A') % 26 + 'A';
36+
cipherText.Append((char)encryptedCharacter);
37+
}
38+
39+
return cipherText.ToString();
40+
}
41+
42+
/// <summary>
43+
/// Removed the key from the encoded string.
44+
/// </summary>
45+
/// <param name="cipherText">The encoded string.</param>
46+
/// <param name="keyword">The key to be removed from the encoded string.</param>
47+
/// <returns>The plaintext (All Uppercase).</returns>
48+
public string Decode(string cipherText, string keyword)
49+
{
50+
cipherText = Regex.Replace(cipherText.ToUpper(CultureInfo.InvariantCulture), "[^A-Z]", string.Empty);
51+
keyword = keyword.ToUpper(CultureInfo.InvariantCulture);
52+
53+
StringBuilder plainText = new StringBuilder();
54+
StringBuilder extendedKeyword = new StringBuilder(keyword);
55+
56+
for(int i = 0; i < cipherText.Length; i++)
57+
{
58+
char cipherCharacter = cipherText[i];
59+
char keywordCharacter = extendedKeyword[i];
60+
61+
int decryptedCharacter = (cipherCharacter - 'A' - (keywordCharacter - 'A') + 26) % 26 + 'A';
62+
plainText.Append((char)decryptedCharacter);
63+
extendedKeyword.Append((char)decryptedCharacter);
64+
}
65+
66+
return plainText.ToString();
67+
}
68+
}
69+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ find more than one implementation for the same objective but using different alg
4141
* [Soundex](./Algorithms/Encoders/SoundexEncoder.cs)
4242
* [Feistel](./Algorithms/Encoders/FeistelCipher.cs)
4343
* [Blowfish](./Algorithms/Encoders/BlowfishEncoder.cs)
44+
* [Autokey](./Algorithms/Encoders/AutokeyEncoder.cs)
4445
* [Graph](./Algorithms/Graph)
4546
* [Minimum Spanning Tree](./Algorithms/Graph/MinimumSpanningTree)
4647
* [Prim's Algorithm (Adjacency Matrix)](./Algorithms/Graph/MinimumSpanningTree/PrimMatrix.cs)

0 commit comments

Comments
 (0)