A guide to creating class libraries in c#: The Caesar Cipher part two

May 07 2012

A guide to creating class libraries in c#: The Caesar Cipher part two


So what`s this caesar cipher thing?

No. It is not a kind of salad. It is a very very simple encryption technique from ancient times. More specifically it is a kind of substitution cipher, and all that means is that it works by replacing each letter of the plaintext with something else, thus making the text unreadable (ciphertext).

What do you mean plaintext and ciphertext?

The time has come to define some simple cryptography lingo:

  1. plaintext : Some kind of message that you want to obscure and make unreadable using encryption.
  2. ciphertext : The message after it has been encrypted. This should be unreadable to anybody who does not possess the decryption key.
  3. key : A secret phrase or other information that is used to encrypt/decrypt plaintext/ciphertext.

Ok, so show me how it works

Its very simple. The algorithm works by swapping each character with another character. It chooses based on a simple table produced by shifting the alphabet by a certain amount. Check out the following example:SHIFT = 4

ABCDEFGHIJKLMNOPQRSTUVWXYZ
WXYZABCDEFGHIJKLMNOPQRSTUV

The idea is very simple. We have used the shift 4, which means we shift the bottom alphabet by 4 characters. So now it begins with WXYZ and ends with V. All we have done is move the last 4 characters to the start. Using these two alphabets to encrypt a plaintext is very simple. You match each character with the top row, and the corresponding character on the bottom row becomes part of the cipher text. Lets look at the following example.PLAINTEXT = SEGFAULT
SHIFT = 4

ABCDEFGHIJKLMNOPQRSTUVWXYZ
WXYZABCDEFGHIJKLMNOPQRSTUV

S = O
E = A
G = C
F = B
A = W
U = Q
L = H
T = P

CIPHERTEXT = OACBWQHP

Totally mixed up heh.
To decrypt the plain text you just flip the alphabets and go through the process again, like this:CIPHERTEXT = OACBWQHP
SHIFT = 4

WXYZABCDEFGHIJKLMNOPQRSTUV
ABCDEFGHIJKLMNOPQRSTUVWXYZ

O = S
A = E
C = G
B = F
W = A
Q = U
H = L
P = T

PLAINTEXT = SEGFAULT

Thats simple enough. Where can I find more information?

Wikipedia has a rather good article on this stuff. Have a look at the caesar ciphers more complex cousin, the Vigenére cipher too.

This is all very well, but where is the code?

In part 3 I will show you how to make this into a class library!


Leave a Reply