Encryption Ciphers — Atbash
Atbash cipher is a substitution cipher that takes the alphabet and maps it in reverse, as you can see from the image above. It was originally used to encrypt the Hebrew alphabet. There are no rounds, keys, cylinder, or anything.
Let’s build this in C#
Prototype
//Gives me an idea of how it should be mapped
//abcdefgh ijklmnopqr stuvwxyz
//zyxwvuts rqponmlkji hgfedcba
//build a variable for the alphabet
string alphaNorm = "abcdefghijklmnopqrstuvwxyz";
//entered some text
string input = "hello world";
string ciphertext = "";
string decryptedtxt = "";
Console.WriteLine(input);
//encryption loop
for (int i = 0; i < input.Length; i++)
{
for (int j = 0; j < alphaNorm.Length; j++)
{
if (input[i] == alphaNorm[j])
{
var temp = alphaNorm.Length - j - 1;
ciphertext += alphaNorm[temp];
}
}
}
Console.WriteLine(ciphertext);
//decryption is the exact same process
for(int i = 0; i < ciphertext.Length; i++)
{
for(int j = 0; j < alphaNorm.Length; j++)
{
if (ciphertext[i] == alphaNorm[j])
{
var temp = alphaNorm.Length - j - 1;
decryptedtxt += alphaNorm[temp];
}
}
}
Console.WriteLine(decryptedtxt);
That worked out well, lets put it into it’s own methods and class. I found classes like StringBuilder, but I want this to be as basic as possible here. The decryption is the exact same function, I called the method cipher instead of making a separate method doing the same thing
class AtBashCipher
{
//defined a static field
static string alphaNorm = "abcdefghijklmnopqrstuvwxyz";
static void Main()
{
//entry point to the program.
Console.WriteLine("Welcome to the AtBash Cipher");
Console.Write("Enter a message: ");
string plaintxt = Console.ReadLine();
string ciphertxt = Cipher(plaintxt);
string decrypttxt = Cipher(ciphertxt);
//printed the messages at various stages
Console.WriteLine("PLAINTXT: " + decrypttxt);
Console.WriteLine("CIPHERTXT: " + ciphertxt);
Console.WriteLine("DECRYPTXT: " + decrypttxt);
}
static string Cipher(string input)
{
string convertedTxt = "";
for(int i = 0; i < input.Length; i++)
{
for (int j = 0; j < alphaNorm.Length; j++)
{
if (input[i] == alphaNorm[j])
{
int reverse = alphaNorm.Length - j - 1;
convertedTxt += alphaNorm[reverse];
}
}
}
return convertedTxt;
}
}
During the inner loop. Takes the length of the alphabet and subtract by the given position. The problem here is it will be off by one index, hence the subtract one.
This is really it. The AtBash cipher isn’t anything fancy, but was created with a purpose, to hide a message, like all ciphers.
Next is the Vigenère Cipher, a Polyalphabetic Substitution Cipher. Thanks for reading.