TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Atbash Cipher

This C# tutorial builds an Atbash cipher. This is useful for obscuring text.

Atbash cipher. An Atbash cipher obscures text.

It is a historical encryption method that substitutes letters. But it is still relevant. There are powerful and modern encryption methods available to every computer user. Atbash is not one of them.

Tip: The Atbash cipher is similar (in design and implementation) to the ROT13 cipher and the Caesar cipher.

ROT13 MethodCaesar Cipher

Example. The Atbash cipher reverses the placement of letters. Each letter A becomes Z, for example, and B becomes Y. The examples here reverse the placement of all lowercase and uppercase letters. Numbers could also be reversed.

Creating new AtbashTable. When you create a new table object, it will generate the lookup table internally. You can call the instance method Transform on any string argument. This will return the cipher text.

Constructor

C# program that implements Atbash cipher

using System;

class Program
{
    static void Main()
    {
	AtbashTable x = new AtbashTable();

	Console.WriteLine(x.Transform("thedeveloperblog.com is awesome."));
	Console.WriteLine(x.Transform(x.Transform("thedeveloperblog.com is awesome.")));

	const string s = "hob hold slow holy horn zoo all irk low glow grog";
	Console.WriteLine(x.Transform(s));
	Console.WriteLine(x.Transform(x.Transform(s)));

	Console.Read();
    }
}

/// <summary>
/// Transforms text using Atbash cipher.
/// </summary>
class AtbashTable
{
    /// <summary>
    /// Lookup table to shift characters.
    /// </summary>
    char[] _shift = new char[char.MaxValue];

    /// <summary>
    /// Generates the lookup table.
    /// </summary>
    public AtbashTable()
    {
	// Set these as the same.
	for (int i = 0; i < char.MaxValue; i++)
	{
	    _shift[i] = (char)i;
	}
	// Reverse order of capital letters.
	for (char c = 'A'; c <= 'Z'; c++)
	{
	    _shift[(int)c] = (char)('Z' + 'A' - c);
	}
	// Reverse order of lowercase letters.
	for (char c = 'a'; c <= 'z'; c++)
	{
	    _shift[(int)c] = (char)('z' + 'a' - c);
	}
    }

    /// <summary>
    /// Apply the Atbash cipher.
    /// </summary>
    public string Transform(string value)
    {
	try
	{
	    // Convert to char array
	    char[] a = value.ToCharArray();
	    // Shift each letter.
	    for (int i = 0; i < a.Length; i++)
	    {
		int t = (int)a[i];
		a[i] = _shift[t];
	    }
	    // Return new string.
	    return new string(a);
	}
	catch
	{
	    // Just return original value on failure.
	    return value;
	}
    }
}

Output

Fnz Nyyra vf pbby.
thedeveloperblog.com is cool.

Hzn Zoovm rh zdvhlnv.
thedeveloperblog.com is awesome.

sly slow hold slob slim all zoo rip old told tilt
hob hold slow holy horn zoo all irk low glow grog

Lookup tables are usually arrays that store different values for each index. Your code will use the indexes to get the values. This can eliminate if-statements and therefore greatly reduce the branching necessary in the CPU.

Note: The lookup table used in the Atbash code seems to improve performance by around two times.

However: It has a startup penalty that might slow down a program's startup time more than is acceptable.

Uses. Ciphers today are mainly used for puzzles. Historically, they were used for important messages and communications, but today they are trivial to break. They are a form of encryption, and therefore have value for learning.

Using lookup tables and learning about simple ciphers can give you insight into real cryptography and more advanced materials. But these ciphers are not useful for online stores or shopping.

Summary. Here we implemented a class in the C# programming language that performs the Atbash transformation on strings. The Atbash cipher provides an example of historical cryptography and its principles.

Note: The code here has several attributes that may be of interest. It is not commercial-grade encryption.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf