### Online Members

Gopi
Pawan Awasthi
Prachi Kulkarni
More...

Resources » Code Snippets » Security

# Encryption and cipher methods (Caesar cipher and Vigenere Cipher)

 Posted Date: 03-Dec-2010 Category: Security Author: Mrinmay Paul Member Level: Gold Points: 25

In this article I will be explaining the very basics of encryption and decryption techniques. I will be discussing two types of encryption/ decryption techniques 1. Caesar Cipher 2. Vigenere Cipher (basic and advanced)

In this article I will be explaining the very basics of encryption and decryption techniques.
Here I will be discussing two types of encryption/ decryption techniques
1. Caesar Cipher
2. Vigenere Cipher (basic and advance)
I will be explaining in detail how the, encryption of basic types, are to be done and I have not used any complex crypto methods for the coding part.
Let's begin

# Caesar Cipher encryption/decryption

A brief history: In cryptography, a Caesar cipher, also known as a Caesar's cipher or the shift cipher or Caesar's code or Caesar shift, is one of the simplest and basic known encryption techniques.
It is a type of replace cipher in which each letter in the plaintext is replaced by a letter with a fixed position separated by a numerical value used as a "key".
Caesar Cipher is or was probably the very first encryption methodology.

Let me give an example:
Let's take a plain text: "DOTNETSPIDER IS A LEADING WEBSITE FOR DOT NET COMMUNITY" and we want to encrypt the plain text using "Caesar Cipher", using key/password as "9". Caesar Cipher only takes numeric password, and only between 0-25.

So our plain text's cipher/encrypted text will become: "MXCWNCBYRMNA RB J UNJMRWP FNKBRCN OXA MXC WNC LXVVDWRCH".

So how did we arrive at the cipher/encrypted text?
I will explain with a simple procedure.
Let us assign each alphabet in our English literature with a numeric value in a tabular format
Such as:

Now take a word from the plain text, for example: "DOTNETSPIDER"
As I have said earlier that it is a type of replace cipher in which each letter in the plaintext is replaced by a letter with a fixed position separated by a numerical value used as a "key".
So we have to take each letter or character,
In case of "DOTNETSPIDER": the value of 'D' is 3, so add 3 by 9 (since we have used '9' as our key/password)
So, 3 + 9 = 12
Now refer to the table above, now whose value is 12, its 'M'.

So here ‘D’ gets replaced by ‘M’
Let’s take another value from “DOTNETSPIDER” ? ‘T’: the value of ‘T’ is 19, so add 19 by 9 (since we have used ‘9’ as our key/password)
So, 19 + 9 = 28,
Here you can see the value is not on the list.
So whenever the value is greater than ‘25’, just subtract the value with ‘26’.
So, 19 + 9 = 28, and now the value is greater than 25, so, 28 – 26 = 2.
Now refer to the table above, now whose value is 2, its ‘C’.
So here ‘T’ gets replaced by ‘C’

Similarly,
O (14) + 9 = 23(X)
T (19) + 9 = 28 – 26 = 2(C)
N (13) + 9 = 22(W)
E (4) + 9 = 13(N)
T (19) + 9 = 28 – 26 = 2(C)
S (18) + 9 = 27 – 26 = 1(B)
P (15) + 9 = 24(Y)
I (8) + 9 = 17(R)
D (3) + 9 = 12(M)
E (4)+ 9 = 13 (N)
R (17) + 9 = 26 – 26 = 0(A)
Hence, “DOTNETSPIDER” became, by using Caesar Cipher “MXCWNCBYRMNA”.

Now let’s go to the coding part.

The look of my app is like this:

After encoding the text.

Now the code:
Encode/Encrypt:
`//Here I am only using characters A-Z(upper case) //To increase the complexity of the ceaser cipher lower case"a-z", Numerics and Symbols also can be used string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; #region Encode private void Encode_Click(object sender, EventArgs e) { string Text = richTextBox1.Text.ToUpper(); int key = Convert.ToInt32( comboBox1.Text); string final = ""; int indexOfChar = 0; char encryptedChar; //Convert/encrypt each and every character of the text foreach (char c in Text) { //Get the index of the character from alphabets variable indexOfChar = alphabets.IndexOf(c); if (c == ' ')//if encounters an white space { final = final + c; } else if(c == '\n')// if encounters a new line { final = final + c; } else if ((indexOfChar + key) > 25)//if the character is at the end of the string "alphabets" { //encrypt the character encryptedChar = alphabets[(indexOfChar + key) - 26]; //add the encrypted character to a string every time to get an encrypted string final = final + encryptedChar; } else { //encrypt the character //add the encrypted character to a string every time to get an encrypted string encryptedChar = alphabets[indexOfChar + key]; final = final + encryptedChar; } } //Display encrypted text richTextBox1.Clear(); richTextBox1.Text = final; } #endregion`

Decode/Decrypt
`#region Decode private void Decode_Click(object sender, EventArgs e) { string Text = richTextBox1.Text.ToUpper(); int key = Convert.ToInt32(comboBox1.Text); string final = ""; int indexOfChar = 0; char decryptedChar; //Convert/decrypt each and every character of the text foreach (char c in Text) { //Get the index of the character from alphabets variable indexOfChar = alphabets.IndexOf(c); if (c == ' ')//if encounters a white space { final = final + c; } else if (c == '\n')// if encounters a new line { final = final + c; } else if ((indexOfChar - key) < 0)//if the character is at the start of the string "alphabets" { //decrypt the character //add the decrypted character to a string every time to get a decrypted string decryptedChar = alphabets[(indexOfChar - key) + 26]; final = final + decryptedChar; } else { //decrypt the character //add the decrypted character to a string every time to get a decrypted string decryptedChar = alphabets[indexOfChar - key]; final = final + decryptedChar; } } //Display decrypted text richTextBox1.Clear(); richTextBox1.Text = final; } #endregion`

Note: Caesar Cipher is very easy to break, as, in the above example, there is only 25 ways to encrypt and decrypt the plain/cipher text, you can include lower case alphabets, symbols and numeric to increase the number of ways, but it’s not a secure way, because it’s too easy to decrypt using trial and error methods.

# Vigenere Cipher Basic

A brief history: The Vigenère cipher is a method of encrypting text by using a series of different Caesar ciphers based on the letters of a keyword.
It is a polyalphabetic cipher based on using successively shifted alphabets, a different shifted alphabet for each of the 26 English letters. The procedure is based on the table shown in Figure below and the use of a keyword. The letters of the keyword determine the shifted alphabets used in the encoding process.

Let me explain with an example:
Let’s take a plain text: “DOTNETSPIDER IS A LEADING WEBSITE FOR DOT NET COMMUNITY” and we want to encrypt the plain text using “Vigenere Cipher”, using key/password as “PAUL”.
So our plain text’s cipher/encrypted text will become: “SONYTTMAXDYC XS U WTAXTCG QPQSCET FIC SON YTT WZBMOYXTS”.

So how did we arrive at the cipher/encrypted text?
I will explain with a simple procedure and mathematical too.
First the simple procedure.
First you have to make the table,
The password for the above example, I have used is “PAUL”.
So from the above table only keep the sections of alphabets of “PASSWORD REFERENCE” that constitutes in the word “PAUL”(in ascending order).
That is: ‘A’, ‘L’, ‘P’, ‘U’

Let’s reduce the size of the table:

Now take a word from the plain text, for example: “DOTNETSPIDER”

Now refer ‘D’ from the “TEXT REFERENCE” section of the table, and ‘P’ from the “PASSWORD REFERENCE” section of the table,
Like this:

See what you get: Now replace the value of ‘D’ with ‘S’

Similarly take the letter ‘O’ from the “TEXT REFERENCE” section of the table, and ‘A’ from the “PASSWORD REFERENCE” section of the table.
(Note: the letters if the text and password should increase by 1 simultaneously, after each encryption of the letters of the plain text)

See what you get: Now replace the value of ‘O’ with ‘O’

Similarly take the letter ‘T’ from the “TEXT REFERENCE” section of the table, and ‘U’ from the “PASSWORD REFERENCE” section of the table.

Now replace the value of ‘T’ with ‘S’

Similarly take the letter ‘N’ from the “TEXT REFERENCE” section of the table, and ‘L’ from the “PASSWORD REFERENCE” section of the table.

Now replace the value of ‘N’ with ‘Y’

Now we have reached at the last letter of the password, so now we have to start over again with the first letter of the password.

Take the letter ‘E’ from the “TEXT REFERENCE” section of the table, and ‘P’ from the “PASSWORD REFERENCE” section of the table.

Now replace the value of ‘E’ with ‘T’

Repeating the above steps you will get “DOTNETSPIDER”, using password as “PAUL”, The Vigenere cipher as, “SONYTTMAXDYC”.

Now let’s derive the Vigenere cipher mathematically:
First of all let’s assign a value to the alphabets, like we did in Caesar cipher above.

For Encryption the formula is:
Value of cipher text letter = (value of plain text letter + value of password text letter) (mod 26)

For Decryption the formula is:
Value of plain text letter = (value of cipher text letter - value of password text letter) (mod 26)

Let’s see with an example:
Plaintext: DOTNETSPIDER

The value of ‘D’ from plain text is 3, and the value of ‘P’ from password is 15.
So using the encryption formula.
(3 + 15) mod 26 = 18 mod 26 = 18.
Now refer to the table above, now whose value is 18, its ‘S’.

Similatly for ‘O’, the value of ‘O’ from plain text is 14, and the value of ‘A’ from password is 0.
So using the encryption formula.
(14 + 0) mod 26 = 14 mod 26 = 14.
Now refer to the table above, now whose value is 14, its ‘O’.

By applying the above formula for encryption, “DOTNETSPIDER” and using password as “PAUL”, The Vigenere cipher becomes, “SONYTTMAXDYC”.

# ********************************************

Vinegere cipher advanced is pretty much same as the basic; the only difference is that we change the structure of the text/password reference table.
In advance Vigenere cipher you will need another password which is known as “alphabet key”.
For example:
Suppose my alphabet key is “SPIDER”
The text/password reference table will be as such:

As you can see the Word “SPIDER” has been set at the first of the text/password reference table, hence this increases the complexity of Vigenere cipher.

“Vinegere Cipher Advanced” depends on how you set your alphabet key in the text/password reference table. It can be in any form such as : you can reverse the alphabet key : “SPIDER” becomes “REDIPS” or you can set the alphabet key at the end of the reference table. There are many way for the advanced version and surely it increases the complexity of the cipher/encrypted text.

In Vinegere Cipher advanced, the text replacement pattern is same as the Vinegere cipher basic, that I have explained above, the only difference is the structure of the text/password reference table.

Let’s encrypt a plain text with Vinegere cipher advanced:
Let’s take a plain text: “DOTNETSPIDER IS A LEADING WEBSITE FOR DOT NET COMMUNITY” and we want to encrypt the plain text using “Vigenere Cipher”, using key/password as “PAUL” and alphabet key as “SPIDER”.

So our plain text’s cipher/encrypted text will become: “EXKERZUMDFYT DA S IRJXNON NQCAWBR MHT EXK ERZ IRNVLEDZQ”.

Do you see it’s very different from the Vinegere cipher basic’s cipher text that we derived earlier.

The process of encrypting the plain text is same as the Vinegere cipher basic (explained above).

Let’s see with an example:
Let’s take plain text as: “WEBSITE”.
The alphabet key is “SPIDER”.

So from the above table only keep the sections of alphabets of “PASSWORD REFERENCE” that constitutes in the word “PAUL”(in ascending order).
That is: ‘P’ ‘A’, ‘L’, and ‘U’

So the derived table would be:

Now refer ‘W’ from the “TEXT REFERENCE” section of the table, and ‘P’ from the “PASSWORD REFERENCE” section of the table,
Like this:

See what you get: Now replace the value of ‘W’ with ‘X’

Similarly take the letter ‘E’ from the “TEXT REFERENCE” section of the table, and ‘A’ from the “PASSWORD REFERENCE” section of the table.

See what you get: Now replace the value of ‘E’ with ‘G’

Repeating the above steps you will get “WEBSITE”, using password as “PAUL” and alphabet key as “SPIDER”, The Vigenere cipher as, “XGPLDZY”.

Now let’s go to the coding part of Vinegere cipher:

The Basic look of my app is as such:

If "Basic" is checked then:

Now the code:

`private void Form1_Load(object sender, EventArgs e) { basicRadioButton.Checked = true; } private void basicRadioButton_CheckedChanged(object sender, EventArgs e) { label2.Visible = false; keyTextBox.Visible = false; } private void advanceRadioButton_CheckedChanged(object sender, EventArgs e) { label2.Visible = true; keyTextBox.Visible = true; }`

`//Here I am only using characters A-Z(upper case) //To increase the complexity of the VIGENERE CIPHER lower case"a-z", Numerics and Symbols also can be used string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";`

Remove duplicate characters from "Alphabet Key" method
`static string RemoveDuplicateChars(string key) { // --- Removes duplicate chars using string concats. --- // Store encountered letters in this string. string text = ""; // Store the result in this string. string final = ""; // Loop over each character. foreach (char value in key) { // See if character is in the table. if (text.IndexOf(value) == -1) { if (value == ' ') { //do nothing } else { // Append to the table and the result. text += value; final += value; } } } return final; }`

Encrypt using "Vigenere Cipher"

`#region Encrypt using "Vigenere Cipher" private void encryptButton_Click(object sender, EventArgs e) { char encryptedChar; string encryptedText = ""; int charIndexOfText, charIndexOfPassword, newIndex; char charOfText; char charOfPassword; string table = ""; string result = ""; alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string text = richTextBox1.Text; string password = passwordTextBox.Text; charIndexOfText = charIndexOfPassword = newIndex = 0; //If want to use both key and password else it will only use password if (advanceRadioButton.Checked) { //Removes duplicate characters from the string "alphabets" //For example: if the key is "MRINMAY" //The string "alphabets" will be modified to "MRINAYBCDEFGHJKLOPQSTUVWXZ" string key = RemoveDuplicateChars(keyTextBox.Text); alphabets = RemoveDuplicateChars(key.ToUpper() + alphabets); } else// If basic radio button is selected { alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } //look for white space in the password and discard it //it is very that, a vigenere cipher's password should not have white space foreach (char value in password) { // if character is white space. if (value == ' ') { //do nothing } else { // Append valid character to the table and the result. table += value; result += value; } //password without white space password = result; } for (int i = 0; i < text.Length; i++) { for (int j = 0; j < password.Length; j++) { if (i < text.Length) { //get each character of the text and the password //Note: I have used only Upper Case alphabets //but if you want to include lower case alphabets, add lower case alphabets(a-z) to string "alphabets" //replace the next two lines of code with this:/* charOfText = text[i]; charOfPassword = password[j];*/ charOfText = char.ToUpper(text[i]); charOfPassword = char.ToUpper(password[j]); if(charOfText == ' ')// if encounters a white space in the text { //add the white case character to the encrypted text encryptedChar = charOfText; encryptedText = encryptedText + encryptedChar; i++; j--; } else if (charOfText == '\n')// if encounters a new line in the text { //add the new line character to the encrypted text encryptedChar = charOfText; encryptedText = encryptedText + encryptedChar; i++; j--; } else { //Get the index of the character of text from alphabets variable charIndexOfText = alphabets.IndexOf(charOfText); //Get the index of the character of password from alphabets variable charIndexOfPassword = alphabets.IndexOf(charOfPassword); //Get the encrypted character newIndex = (charIndexOfText + charIndexOfPassword) % 26; encryptedChar = alphabets[newIndex]; //add the encrypted character to a string every time to get an encrypted string encryptedText = encryptedText + encryptedChar; i++; } } } i--; } //Display Encrypted text richTextBox1.Clear(); richTextBox1.Text = encryptedText; } #endregion`

Decrypt using "Vigenere Cipher"
`#region Decrypt using "Vigenere Cipher" private void Decrypt_Click(object sender, EventArgs e) { char newChar; string encryptedText = richTextBox1.Text; string decryptedText = ""; int charIndexOfEncryptedText, charIndexOfPassword, newIndex; char charOfEncryptedText; char charOfPassword; string table = ""; string result = ""; alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string password = passwordTextBox.Text; charIndexOfEncryptedText = charIndexOfPassword = newIndex = 0; //If want to use both key and password else it will only use password if (advanceRadioButton.Checked) { //Removes duplicate characters from the string "alphabets" //For example: if the key is "MRINMAY" //The string "alphabets" will be modified to "MRINAYBCDEFGHJKLOPQSTUVWXZ" string key = RemoveDuplicateChars(keyTextBox.Text); alphabets = RemoveDuplicateChars(key.ToUpper() + alphabets); } else// If basic radio button is selected { alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } //look for white space in the password and discard it //it is very that, a vigenere cipher's password should not have white space foreach (char value in password) { // See if character is in the table. if (value == ' ') { //do nothing } else { // Append valid character to the table and the result. table += value; result += value; } //password without white space password = result; } for (int i = 0; i < encryptedText.Length; i++) { for (int j = 0; j < password.Length; j++) { if (i < encryptedText.Length) { //get each character of the encrypted text and the password //Note: I have used only Upper Case alphabets //but if you want to include lower case alphabets, add lower case alphabets(a-z) to string "alphabets" //replace the next two lines of code with this:/* charOfText = encryptedtext[i]; charOfPassword = password[j];*/ charOfEncryptedText = char.ToUpper(encryptedText[i]); charOfPassword = char.ToUpper(password[j]); if (charOfEncryptedText == ' ')// if encounters a white space in the encrypted text { //add the white case character to the decrypted text newChar = charOfEncryptedText; decryptedText = decryptedText + newChar; i++; j--; } else if (charOfEncryptedText == '\n')// if encounters a new line in the encrypted text { //add the new line character to the decrypted text newChar = charOfEncryptedText; decryptedText = decryptedText + newChar; i++; j--; } else { //Get the index of the character of encrypted text from alphabets variable charIndexOfEncryptedText = alphabets.IndexOf(charOfEncryptedText); //Get the index of the character of password from alphabets variable charIndexOfPassword = alphabets.IndexOf(charOfPassword); newIndex = (charIndexOfEncryptedText - charIndexOfPassword) % 26; //if the character is at the start of the string "alphabets" if ((charIndexOfEncryptedText - charIndexOfPassword) < 0) { //Get the decrypted character newChar = alphabets[newIndex + 26]; //add the decrypted character to a string every time to get an encrypted string decryptedText = decryptedText + newChar; i++; } else { //Get the decrypted character newChar = alphabets[newIndex]; //add the decrypted character to a string every time to get an decrypted string decryptedText = decryptedText + newChar; i++; } } } } i--; } richTextBox1.Clear(); richTextBox1.Text = decryptedText; } #endregion`

# ******************************************

Note: In the above examples I have only used upper case English alphabets (A-Z).
You can use lower case alphabets(a-z), numerics(0-9) and symbols to increase the complexity of the ciphers.

# ************** THE END ******************

I am also attaching the applications I discussed here.

If you have any doubts, give a response.

Cheers
Paul

Attachments
• Caesar Cipher Application (41491-3351-caesar-cipher.rar)
• Vigenere Cipher Application (41491-6842-Vigenere-Cipher.rar)

• Did you like this resource? Share it with your friends and show your love!

Responses to "Encryption and cipher methods (Caesar cipher and Vigenere Cipher)"
Guest Author: rezai     19 Mar 2012
hi.
can you say what encryption is this?
http://ashiyane.org/forums/attachment.php?attachmentid=47409&d=1332222848
thanks.

Author: Mrinmay Paul    23 Mar 2012Member Level: Gold   Points : 0
Hi Rezai,

The URL that you mentioned here redirects me to a page in Persian language, I don't speak Persian.
What exactly is your question ?

Paul

Author: Mrinmay Paul    23 Mar 2012Member Level: Gold   Points : 0
Hi Rezai,

The URL that you mentioned here redirects me to a page in Persian language, I don't speak Persian.
What exactly is your question ?

Paul

Feedbacks

## Post Comment:

• Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
• No HTML formatting and links to other web sites are allowed.
• This is a strictly moderated site. Absolutely no spam allowed.
•  Name: Sign In to fill automatically. Email: (Will not be published, but required to validate comment) Type the numbers and letters shown on the left.

 Next Resource: Encryption Decryption Implementation Hazards Return to Resources Post New Resource Category: Security

Post resources and earn money!

More Resources
 Popular Tags Tag posting guidelines Search Tags
Caesar cipher  .  Vinegere Cipher  .  Encryption and cipher methods (Caesar cipher and Vigenere Cipher)  .  Vigenere Cipher  .  Encryption and cipher methods  .  Traditional cipher  .  Traditional encryption  .