Skip to content

Add Autokey encoder #509

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

Merged
merged 2 commits into from
Jul 11, 2025
Merged
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 static class AutokeyEncoderTests
{
[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));
}
}
}
69 changes: 69 additions & 0 deletions Algorithms/Encoders/AutokeyEncorder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Globalization;
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(CultureInfo.InvariantCulture), "[^A-Z]", string.Empty);
keyword = keyword.ToUpper(CultureInfo.InvariantCulture);

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(CultureInfo.InvariantCulture), "[^A-Z]", string.Empty);
keyword = keyword.ToUpper(CultureInfo.InvariantCulture);

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