Thursday, June 24, 2010

Caesar Cipher in C#

The Caesar cipher was one of the first encryption methods of the world, as the name alreadys says, it was used already by Julius Caesar to send private information. In this method, alle letters are shifted circularly
by a fixed number.
Computer scientists call this a monoalphabetical substitution, meaning the same fixed alphabet is used for plain- and ciphertext and all letters of the plaintext are replaced by different letters.
An example for the Caesar cipher: Let the plaintext be "zum" and the shift key be "b" (2). Now every letter of the plaintext is shifted by 2 positions, after "z" we start again at "a".
So "z" becomes "b", "u" becomes "w" and "m" becomes "o". The ciphertext then is "bwo".
I implemented the Caesar cipher in C#, what I want to present you now.
I hope the sourcecode is well documented, so I will just post it (System.Text.RegularExpressions has to be included):
class CaesarCiphering
{
bool EliminateBlanks; // true if blanks shall be deleted
char Offset; // the shift key

public CaesarCiphering(bool eliminateBlanks, char offset)
{
EliminateBlanks = eliminateBlanks;
Offset = offset;
}

/// <summary>
/// in this function the given string is cleaned, meaning all letters not from the
/// encryption alphabet (only the letters a - z in lower case are allowed)
/// and if wanted blanks are eliminated
/// </summary>
private string CleanString(string text)
{
text = text.ToLower(); // convert text to lower case

if (EliminateBlanks) // eliminate blanks
text = text.Replace(" """);

// the following regular expression describes all strings which
// do not contain entirely out of a-z and blanks
Regex NoValidCharacters = new Regex("[^a-z\\s]*");
// delete all matching textparts
text = NoValidCharacters.Replace(text, "");

return text;
}

/// <summary>
/// decrypts the given string
/// </summary>
public string Encode(string plainText)
{
string CipherText = "";

// clean plainText
plainText = CleanString(plainText);

// Replace every letter in plainText by the corresponding cipher letter.
// The following checks whether the current letter is a blank, if yes it is kept,
// otherwise the cipher character is calculated.
foreach (char c in plainText)
CipherText += (c == ' ') ? ' ' : (char)(((c - 96) + (Offset - 96) + 25) % 26 + 97);

return CipherText;
}

public string Decode(string cipherText)
{
string PlainText = "";

// clean cipherText
cipherText = CleanString(cipherText);

// Replace every letter from cipherText by its corresponding plaintext letter.
// Again the following keeps blanks and calculates the plaintext letter otherwise.
foreach (char c in cipherText)
PlainText += (c == ' ') ? ' ' : (char)(((c - 96) - (Offset - 96) + 25) % 26 + 97);

return PlainText;
}

}

An exemplary call of the Caesar encryption could look as follows:

CaesarCiphering Caesar = new CaesarCiphering(false, 'b');
string CipherText = Caesar.Encode("zum");
string PlainText = Caesar.Decode(CipherText);

No comments:

Post a Comment