Share via


C# - Simple Ciphers

 


Overview

 

This example shows two types of classical ciphers. These ciphers have been around since Ancient Greek or Roman times. These are Substitution Ciphers (sometimes known as Ceasar Ciphers, as Julius Ceasar used a Substitution Cipher), and Transposition Ciphers.

 

 

In a substitution cipher, letters are systematically replaced throughout the message for other letters. The key used is also known as a shift. Using a key of 3 (which Ceasar did), an 'A' becomes a 'D', a 'B' becomes an 'E' and so on.
To decipher a Substitution Cipher, you need the key and the Ciphertext, although these days it's easy to crack a simple Substitution Cipher. 

 


 

 

A Transposition Cipher uses a keyword. In this example I've used the key 'ALGORITHMS' which is a 10 character word with no repeated characters. The key is written to a grid with 10 columns. starting in the second row of the grid the plaintext is entered left to right cell by cell wrapping to new rows as necessary. The columns of the grid are then sorted randomly. So the Ciphertext is an anagram of the Plaintext.
To decipher a Transposition Cipher, you need the keyword, and all of the scrambled characters from the grid. Rearranging the columns with the correctly spelled keyword, reveals the Plaintext. These Ciphers are also fairly easy to crack.

 


The Code

 

Substitution

 

private void  encryptSubstitution()
{
    String plainText = this.textBox1.Text.ToLower();
    String cipherText = "";
    for (int x = 0; x <= plainText.Length - 1; x++)
    {
        char c = plainText[x];
        if (((int)c >= 97 && (int)c < 97 + 26))
        {
            int i = ((int)c) - 97;
            cipherText += ((char)(65 + ((i + (int)this.numericUpDown1.Value) % 26))).ToString();
        }
        else
        {
            cipherText += c.ToString();
        }
    }
    this.textBox2.Text = cipherText;
}
 
private void  decryptSubstitution()
{
    String plainText = "";
    String cipherText = this.textBox2.Text;
    for (int x = 0; x <= cipherText.Length - 1; x++)
    {
        char c = cipherText[x];
        if (((int)c >= 65 && (int)c < 65 + 26))
        {
            int i = ((int)c) - 65;
            plainText += ((char)(97 + ((26 + i - (int)this.numericUpDown1.Value) % 26))).ToString();
        }
        else
        {
            plainText += c.ToString();
        }
    }
    this.textBox3.Text = plainText;
}

Transposition

 

private void  encryptTransposition()
{
    List<String[]> newGrid = new  List<String[]>();
    for (int r = 0; r <= 9; r++)
    {
        newGrid.Add(new String[10]);
        for (int x = 0; x <= 1; x++)
        {
            newGrid[r][x] = this.dataGridView1.Rows[r].Cells[x + 8].Value.ToString();
            newGrid[r][x + 8] = this.dataGridView1.Rows[r].Cells[x].Value.ToString();
        }
        for (int x = 2; x <= 3; x++)
        {
            newGrid[r][x] = this.dataGridView1.Rows[r].Cells[x + 4].Value.ToString();
            newGrid[r][x + 4] = this.dataGridView1.Rows[r].Cells[x].Value.ToString();
        }
        newGrid[r][4] = this.dataGridView1.Rows[r].Cells[5].Value.ToString();
        newGrid[r][5] = this.dataGridView1.Rows[r].Cells[4].Value.ToString();
        this.dataGridView1.Rows[r].SetValues(newGrid[r]);
    }
    for (int r = 1; r <= 9; r++)
    {
        for (int c = 0; c <= 9; c++)
        {
            this.textBox5.Text += this.dataGridView1.Rows[r].Cells[c].Value.ToString();
        }
    }
}
 
private void  decryptTransposition()
{
    List<Point> indices = new  List<Point>();
    for (int y = 9; y >= 0; y--)
    {
        char c = this.textBox7.Text[y];
        for (int x = 0; x <= 9; x++)
        {
            if(this.dataGridView1.Rows[0].Cells[x].Value.ToString().Equals(c.ToString()))
            {
                indices.Add(new Point(x, y));
            }
        }
    }
 
    List<String[]> newGrid = new  List<String[]>();
    for (int r = 1; r <= 9; r++)
    {
        newGrid.Add(new String[10]);
        for (int x = 0; x <= 9; x++)
        {
            newGrid[r - 1][indices[x].X] = this.dataGridView1.Rows[r].Cells[indices[x].Y].Value.ToString();
            newGrid[r - 1][indices[x].Y] = this.dataGridView1.Rows[r].Cells[indices[x].X].Value.ToString();
        }                               
    }
    for (int r = 1; r <= 9; r++)
    {
        for (int c = 0; c <= 9; c++)
        {
            this.textBox4.Text += newGrid[r - 1][c].ToLower();
        }
    }
}

 


Conclusion

 

Modern Encryption has advanced far beyond these primitive ancient methods in the computer age, but it's interesting to discover the beginnings of a technology.

 


Downloads

 

Download here...