Caesar Cipher In C#

To understand the mess of Information Security is beyond the scope of my article, but I can give you a partial understanding by taking  a resourceful model of information security. We are dealing with security. So, it is necessary to understand the basic terminology that we will be using in our article.

  • Plaintext - original message
  • Ciphertext - coded message
  • cipher - algorithm for transforming plaintext to ciphertext
  • key - info used in cipher known only to sender/receiver
  • encipher - (encrypt) converting plaintext to ciphertext
  • decipher - (decrypt) recovering ciphertext from plaintext
  • cryptography - study of encryption principles/methods
  • cryptanalysis - (code-breaking) study of principles/ methods of deciphering ciphertext without knowing key
  • cryptology - field of both cryptography and cryptanalysis

For any secure communication, there is a need for three things - Encryption Algorithm, Decryption Algorithm, and a Key. The key part can be a public or private key depending upon which model you are working on – Symmetric or Asymmetric Model.

We are taking here about the Symmetric Cipher Model which really is very easy to understand. It has a plaintext that is to be encrypted in Ciphertext via some encryption algorithm, and sent via a secure channel to the receiver. At the receiver part, this Ciphertext gets decrypted by some decryption algorithm, so that the receiver can read the message. The below image will help you understand the Symmetric Cipher Model.

Symmetric Cipher Model


Here, you can see that a plaintext is ready to be sent to the receiver. It will first go to the Encryption algorithm where a secret key is also taking part with algorithm. Now, this key is actually shared between sender and receiver, and no other entity other than sender or receiver must get the key. 

Now, when these three things (plaintext, encryption algorithm and the key) complete their individual work i.e. to encrypt the plaintext – Ciphertext, the Ciphertext gets transmitted to the receiver.

Here, it goes via a decryption algorithm which also needs the same shared key that is there on the sender side. If you don’t have the same key, there is no other way you can get the plaintext.

We call it Cryptography – That actually makes the operation to transfer the plaintext into the Ciphertext. It uses two approaches to do this.
  1. Substitution Method
  2. Transposition Method

You can characterize cryptographic systems by checking out the below points.

  • Type of encryption operations used
    Substitution Method or Transposition Method

  • Number of keys used
    single-key or private / two-key or public ( Symmetric or Asymmetric)

  • way in which plaintext is processed
    Block Cipher / Stream Cipher

We are discussing here, the Caesar Cipher that comes under the Substitution Method.

So, what is Substitution Method?

Substitution Method is where letters of plaintext are replaced by other letters or by numbers or symbols or if plaintext is viewed as a sequence of bits, then substitution involves replacing plaintext bit patterns with Ciphertext bit patterns.

Substitution techniques involve:

  1. Caesar Cipher
  2. Mono- alphabetic Cipher
  3. Playfair Cipher
  4. Hill Cipher
  5. Polyalphabetic Cipher

The Caesar Cipher involves replacing each letter of the alphabet with the letter – standing places down or up according to the key given.

Suppose “M” is the plaintext and the key is given as 4, then you get the Ciphertext as the letter “Q”. In a similar way “A” is the plaintext then Ciphertext will be – “E”.

A better way to understand



Algorithm For Encryption

For each plaintext (P), Ciphertext (C) is.

C = E (3, P) = (P+3) mod 26

For Decryption

P = D (3, C) = (C-3) mod 26

In general  [C= E (K, P) = (P+K) mod 26]
[P = D (K, C) = (C-K) mod 26], where

P Plaintext
C Ciphertext
E Encryption Algorithm
D Decryption Algorithm
K Key

Three important characteristic of this problem enable us to use brute-force cryptoanalysis.

  1. Encryption and Decryption algorithms are known.
  2. There are only 25 keys to try.
  3. Language of plaintext is known and easily recognizable.

Implementation of Caesar Cipher in C#

Step 1

Open your Visual Studio. By pressing Ctrl +Shift + N, you will get your “New Project” window.



Step 2

After pressing OK, you will get into your Coding Part where you will see three files in Solution Explorer [Properties, References, Program.cs]. Program.cs file is your main file where you embed all your "making a change" program code.


Logic of Encryption

  1. public static string Encipher(string input, int key)  
  2. {  
  3.     string output = string.Empty;  
  4.   
  5.     foreach(char ch in input)  
  6.     output += cipher(ch, key);  
  7.   
  8.     return output;  
  9. }  
Logic of Decryption
  1. public static string Decipher(string input, int key)  
  2. {  
  3.     return Encipher(input, 26 - key);  
  4. }  
Full Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Caesar_Cipher {  
  7.     class Program {  
  8.   
  9.   
  10.         public static char cipher(char ch, int key) {  
  11.             if (!char.IsLetter(ch)) {  
  12.   
  13.                 return ch;  
  14.             }  
  15.   
  16.             char d = char.IsUpper(ch) ? 'A' : 'a';  
  17.             return (char)((((ch + key) - d) % 26) + d);  
  18.   
  19.   
  20.         }  
  21.   
  22.   
  23.         public static string Encipher(string input, int key) {  
  24.             string output = string.Empty;  
  25.   
  26.             foreach(char ch in input)  
  27.             output += cipher(ch, key);  
  28.   
  29.             return output;  
  30.         }  
  31.   
  32.         public static string Decipher(string input, int key) {  
  33.             return Encipher(input, 26 - key);  
  34.         }  
  35.   
  36.   
  37.         static void Main(string[] args) {  
  38.   
  39.             Console.WriteLine("Type a string to encrypt:");  
  40.             string UserString = Console.ReadLine();  
  41.   
  42.             Console.WriteLine("\n");  
  43.   
  44.             Console.Write("Enter your Key");  
  45.             int key = Convert.ToInt32(Console.ReadLine());  
  46.             Console.WriteLine("\n");  
  47.   
  48.   
  49.             Console.WriteLine("Encrypted Data");  
  50.   
  51.             string cipherText = Encipher(UserString, key);  
  52.             Console.WriteLine(cipherText);  
  53.             Console.Write("\n");  
  54.   
  55.             Console.WriteLine("Decrypted Data:");  
  56.   
  57.             string t = Decipher(cipherText, key);  
  58.             Console.WriteLine(t);  
  59.             Console.Write("\n");  
  60.   
  61.   
  62.   
  63.   
  64.             Console.ReadKey();  
  65.   
  66.         }  
  67.     }  
  68. }  
Output Window



Hope you liked it! Thank you for reading! Have a good day!

Up Next
    Ebook Download
    View all
    Learn
    View all