Monday, 6 June 2016

Introduction to Cryptography and Caesar Cipher

Hello everyone. In this series I will give you the idea about different Cryptographic Algorithms used for encryption and decryption of messages. I will provide the codes that 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 that can be only read by the intended recipient.

It involves two main processes - Encryption and Decryption.
Encryption - It is the technique of converting 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 :
1. Technique Based
2. Key Based

1. Technique Based can be further divided into two parts :
a) Substitution Techniques
b) Transposition Techniques

2. Key Based can also be divided into two parts :
a) Symmetric Key Cryptography
b) Asymmetric Key Cryptography

And it goes on when go deeper into these topics.

But for the 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 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 digits!!

Here is the code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfAppCryptography.Algorithms.TechniqueBased.Substitution
{
    public class CaesarCipher
    {
        public string Encrypt(string plaintext)
        {
            int av, oav, cav, cv;
            string ciphertext = null;
            char[] ptca = plaintext.ToCharArray();
            char[] cc = new char[500];
            for(int i = 0; i < ptca.Length; i++)
            {
                if (ptca[i] == ' ')
                {
                    cc[i] = ptca[i];
                }
                else
                {
                    av = ptca[i];
                    oav = av - 97;
                    cav = (oav + 3) % 26;
                    cv = cav + 97;
                    cc[i] = (char)cv;
                }
                ciphertext += cc[i];
            }
            return ciphertext;
        }
        public string Decrypt(string ciphertext)
        {
            int av, oav, cav, cv;
            string plaintext = null;
            char[] ctca = ciphertext.ToCharArray();
            char[] cc = new char[500];
            for (int i = 0; i < ctca.Length; i++)
            {
                if (ctca[i] == ' ')
                {
                    cc[i] = ctca[i];
                }
                else
                {
                    av = ctca[i];
                    oav = av - 97;
                    cav = (oav - 3) % 26;
                    cv = cav + 97;
                    cc[i] = (char)cv;
                }
                plaintext += cc[i];
            }
            return plaintext;
        }
    }
}







I hope you find my blog interesting. Keep following me and keep coding.

Practice Page Link :
http://handsoncrypto.azurewebsites.net/PracticePage.aspx

4 comments: