Introduction To Cryptography And Caesar Cipher

Hello everyone. In this series I will give you an idea about different Cryptographic Algorithms, used for encryption and decryption of the messages. I will provide the codes, which are written in C#.

First of all, what is Cryptography?

Cryptography is the technique of storing and sending some information, by transforming the original message into something, which can be only read by the intended recipient.

It involves two main processes - Encryption and Decryption.

  • Encryption - It is the technique to convert the plain text into cipher text (encrypted text) by the use of some algorithm.
  • Decryption - It is exactly the reverse of Encryption process, which converts the cipher text to plain text.

There are basically two approaches, which are:

  1. Technique Based
  2. Key Based

Technique Based can be further divided into two parts, namely:

  • Substitution Techniques
  • Transposition Techniques

Key Based can also be divided into two parts, which are:

  • Symmetric Key Cryptography
  • Asymmetric Key Cryptography

It goes on, when you go deeper into these topics.

For simplicity, I will start with the basic algorithms like Substitution techniques. From the name itself, it is clear that each character in the plain text is replaced with another. In this technique, I will give you an idea about many algorithms - Caesar Cipher, Modified Caesar Cipher, Mono-alphabetic Cipher, Poly-alphabetic Cipher, Homophonic Cipher and many more.

Here, I will start with Caesar Cipher - the simplest of all.

In this algorithm, 3 is added with each character to get the new character thereby giving the cipher text. Adding 3 with each character is the Encryption process. Similarly, Decryption is the reverse process i.e. subtracting 3 from each character to get the plain text.

For example,

Plain Text : h e l l o
Cipher Text : k h o o r

For simplicity, I have considered only the lower case alphabets but you can make it more robust by considering all lower case, upper case alphabets and also the digits!!

Here is the code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using WpfAppCryptography.ViewModel;  
  7. namespace WpfAppCryptography.Algorithms.TechniqueBased.Substitution {  
  8.     public class CaesarCipher: Algorithm {  
  9.         public new string AlgorithmName = "Caesar Cipher";  
  10.         public override string Encrypt(string plaintext, string key) {  
  11.             int av, oav, cav, cv;  
  12.             string ciphertext = null;  
  13.             char[] ptca = plaintext.ToCharArray();  
  14.             char[] cc = new char[500];  
  15.             for (int i = 0; i < ptca.Length; i++) {  
  16.                 if (ptca[i] == ' ') {  
  17.                     cc[i] = ptca[i];  
  18.                 } else {  
  19.                     av = ptca[i];  
  20.                     oav = av - 97;  
  21.                     cav = (oav + 3) % 26;  
  22.                     cv = cav + 97;  
  23.                     cc[i] = (char) cv;  
  24.                 }  
  25.                 ciphertext += cc[i];  
  26.             }  
  27.             return ciphertext;  
  28.         }  
  29.         public override string Decrypt(string ciphertext, string key) {  
  30.             int av, oav, cav, cv;  
  31.             string plaintext = null;  
  32.             char[] ctca = ciphertext.ToCharArray();  
  33.             char[] cc = new char[500];  
  34.             for (int i = 0; i < ctca.Length; i++) {  
  35.                 if (ctca[i] == ' ') {  
  36.                     cc[i] = ctca[i];  
  37.                 } else {  
  38.                     av = ctca[i];  
  39.                     oav = av - 97;  
  40.                     cav = (oav - 3) % 26;  
  41.                     cv = cav + 97;  
  42.                     cc[i] = (char) cv;  
  43.                 }  
  44.                 plaintext += cc[i];  
  45.             }  
  46.             return plaintext;  
  47.         }  
  48.     }  
  49. }  
output

output

output

output

Keep following me and keep coding. 

Up Next
    Ebook Download
    View all
    Learn
    View all