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:
- Technique Based
- 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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using WpfAppCryptography.ViewModel;
- namespace WpfAppCryptography.Algorithms.TechniqueBased.Substitution {
- public class CaesarCipher: Algorithm {
- public new string AlgorithmName = "Caesar Cipher";
- public override string Encrypt(string plaintext, string key) {
- 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 override string Decrypt(string ciphertext, string key) {
- 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;
- }
- }
- }
Keep following me and keep coding.