Skip to content

Add Autokey encoder #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Algorithms.Tests/Encoders/AutokeyEncoderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Algorithms.Encoders;
using NUnit.Framework;
using NUnit.Framework.Internal;

namespace Algorithms.Tests.Encoders
{
public class AutokeyEncoderTests

Check warning on line 8 in Algorithms.Tests/Encoders/AutokeyEncoderTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms.Tests/Encoders/AutokeyEncoderTests.cs#L8

Add a 'protected' constructor or the 'static' keyword to the class declaration.
{
[Test]
public static void DecodedStringIsTheSame()
{
// Arrange
var plainText = "PLAINTEXT";
var keyword = "KEYWORD";
var encoder = new AutokeyEncorder();

// Act
var encoded = encoder.Encode(plainText, keyword);
var decoded = encoder.Decode(encoded, keyword);

// Assert
Assert.That(decoded, Is.EqualTo(plainText));
}
}
}
68 changes: 68 additions & 0 deletions Algorithms/Encoders/AutokeyEncorder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Algorithms.Encoders
{
/// <summary>
/// Class for AutoKey encoding strings.
/// </summary>
public class AutokeyEncorder
{
/// <summary>
/// Autokey Cipher is a type of polyalphabetic cipher.
/// This works by choosing a key (a word or short phrase),
/// then you append the plaintext to itself to form a longer key.
/// </summary>
/// <param name="plainText">The string to be appended to the key.</param>
/// <param name="keyword">The string to be appended to the plaintext.</param>
/// <returns>The Autokey encoded string (All Uppercase).</returns>
public string Encode(string plainText, string keyword)
{
plainText = Regex.Replace(plainText.ToUpper(), "[^A-Z]", string.Empty);

Check notice on line 22 in Algorithms/Encoders/AutokeyEncorder.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Encoders/AutokeyEncorder.cs#L22

Define the locale to be used in this string operation.
keyword = keyword.ToUpper();

Check notice on line 23 in Algorithms/Encoders/AutokeyEncorder.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Encoders/AutokeyEncorder.cs#L23

Define the locale to be used in this string operation.

keyword += plainText;

StringBuilder cipherText = new StringBuilder();

for(int i = 0; i < plainText.Length; i++)
{
char plainCharacter = plainText[i];
char keyCharacter = keyword[i];

int encryptedCharacter = (plainCharacter - 'A' + keyCharacter - 'A') % 26 + 'A';
cipherText.Append((char)encryptedCharacter);
}

return cipherText.ToString();
}

/// <summary>
/// Removed the key from the encoded string.
/// </summary>
/// <param name="cipherText">The encoded string.</param>
/// <param name="keyword">The key to be removed from the encoded string.</param>
/// <returns>The plaintext (All Uppercase).</returns>
public string Decode(string cipherText, string keyword)
{
cipherText = Regex.Replace(cipherText.ToUpper(), "[^A-Z]", string.Empty);

Check notice on line 49 in Algorithms/Encoders/AutokeyEncorder.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Encoders/AutokeyEncorder.cs#L49

Define the locale to be used in this string operation.
keyword = keyword.ToUpper();

StringBuilder plainText = new StringBuilder();
StringBuilder extendedKeyword = new StringBuilder(keyword);

for(int i = 0; i < cipherText.Length; i++)
{
char cipherCharacter = cipherText[i];
char keywordCharacter = extendedKeyword[i];

int decryptedCharacter = (cipherCharacter - 'A' - (keywordCharacter - 'A') + 26) % 26 + 'A';
plainText.Append((char)decryptedCharacter);
extendedKeyword.Append((char)decryptedCharacter);
}

return plainText.ToString();
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ find more than one implementation for the same objective but using different alg
* [Soundex](./Algorithms/Encoders/SoundexEncoder.cs)
* [Feistel](./Algorithms/Encoders/FeistelCipher.cs)
* [Blowfish](./Algorithms/Encoders/BlowfishEncoder.cs)
* [Autokey](./Algorithms/Encoders/AutokeyEncoder.cs)
* [Graph](./Algorithms/Graph)
* [Minimum Spanning Tree](./Algorithms/Graph/MinimumSpanningTree)
* [Prim's Algorithm (Adjacency Matrix)](./Algorithms/Graph/MinimumSpanningTree/PrimMatrix.cs)
Expand Down