Encryption and cipher methods (Caesar cipher and Vigenere Cipher)

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 techniques1. Caesar Cipher2. 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 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 formatSuch 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 = 12Now 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’

//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";

//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; } }

//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; } }

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.

End of Caesar Cipher

*****************************************

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”And the password is “PAUL”.

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.

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”.

End of Vigenere cipher basic

********************************************

Vigenere cipher advance

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”.

//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";

//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; }

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);

//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);