Caesar Cipher Substitution Technique in C#

Caesar Cipher is one of the substitution technique which is used encrypt only the alphabets. In Caesar Cipher, all plain text letters shift to a specific number of positions. For example, if the plain text has letter “A” and if the shift key is “4” then the letter shift to “E”.  Let’s number the alphabets as shown below. 

A a

1

B b

2

C c

3

D d

4

E e

5

F f

6

G g

7

H h

8

I i

9

J j

10

K k

11

L l

12

M m

13

N n

14

O o

15

P p

16

Q q

17

R r

18

S s

19

T t

20

U u

21

V v

22

W w

23

X x

24

Y y

25

Z z

26

As shown above, we provide the numbers for each alphabet, so that we can quickly shift each letter with the particular shift key. Let’s have plain text “This is a Plain Text”, if we change this text with shift key as 4 then the plain text becomes “Xlm mw e Tpemr Xibx” as Cipher text. If you observe cipher text, it converts only letters and it won’t convert spaces. Caesar cipher converts only alphabets characters, and it won’t convert numbers, spaces or any other special characters. The import thing while implementing the Caesar Cipher is we can use shift keys from 1 to 25 only because alphabets are only 26.

In this article, we discuss Caesar Cipher in C# with a simple example. Open Microsoft Visual Studio, and create C# Console Application and name it as CSharpCaesarCipher. Add below code, and this example uses shift key 4 to convert the plain text into cipher text.

using System;

namespace CSharpCaesarCipher

{

    class Program

    {

        //shift key should be between 1 and 25

        static int SHIFT_KEY = 4; 

        static void Main(string[] args)

        {

            Console.WriteLine("Please enter text for caesar encryption");

            string sInput = Console.ReadLine(); 

            string sCipherText = CaesarEncryption(sInput);

            string sOrginalText = CaesarDecryption(sCipherText); 

            Console.WriteLine("------------------------------------");

            Console.WriteLine("Cipher Text after Caesar Encryption: {0}", sCipherText);

            Console.WriteLine("Plain Text after Caesar Decryption: {0}", sOrginalText);

            Console.ReadLine();

        } 

        /// <summary>

        /// Encrypts the input text

        /// </summary>

        /// <param name="sPlainText">Plain Text</param>

        /// <returns></returns>

        static string CaesarEncryption(string sPlainText)

        {

            string sEncryptedText = string.Empty;

            char outChar, Lower_Limt, Upper_Limit; 

            char[] chrs = sPlainText.ToCharArray();

            foreach (Char ch in chrs)           

            {

                if (Char.IsLetter(ch) == false)

                {

                    outChar = ch;

                }

                else

                {

                    outChar = (char)(ch + SHIFT_KEY); 

                    if (Char.IsLower(ch))

                    {

                        Lower_Limt = 'a';

                        Upper_Limit = 'z';

                    }

                    else

                    {

                        Lower_Limt = 'A';

                        Upper_Limit = 'Z';

                    } 

                    if (outChar > Upper_Limit)

                    {

                        outChar = (char)(outChar - 26);

                    }

                    else if (outChar < Lower_Limt)

                    {

                        outChar = (char)(outChar + 26);

                    }

                }

                sEncryptedText = sEncryptedText + outChar;

            }

            return sEncryptedText;

        } 

        /// <summary>

        /// Decrypts the cipher text

        /// </summary>

        /// <param name="sCipherText">Cipher Text</param>

        /// <returns></returns>

        static string CaesarDecryption(string sCipherText)

        {

            string sDecryptedText = string.Empty;

            char outChar, Lower_Limt, Upper_Limit; 

            char[] chrs = sCipherText.ToCharArray();

            foreach (Char ch in chrs)

            {

                if (Char.IsLetter(ch) == false)

                {

                    outChar = ch;

                }

                else

                {

                    outChar = (char)(ch - SHIFT_KEY); 

                    if (Char.IsLower(ch))

                    {

                        Lower_Limt = 'a';

                        Upper_Limit = 'z';

                    }

                    else

                    {

                        Lower_Limt = 'A';

                        Upper_Limit = 'Z';

                    } 

                    if (outChar > Upper_Limit)

                    {

                        outChar = (char)(outChar - 26);

                    }

                    else if (outChar < Lower_Limt)

                    {

                        outChar = (char)(outChar + 26);

                    }

                }

                sDecryptedText = sDecryptedText + outChar;

            }

            return sDecryptedText;

        }

    }

} 

If you observe the above code, CaesarEncryption() method converts only alphabets (upper case and lower case) and will not convert any other special characters. Run the application and enter plain text as “This is Raj and My Id is 001” and it converts the plain text into cipher text as shown below.

Caesar Cipher is very simple to implement, but it has several disadvantages.

Caesar Cipher Disadvantages:

  • It is very simple to decrypt the Caesar cipher text. There are only 25 shift keys to try to decrypt the cipher text. Hackers can easily decrypt the cipher text into plain text just by trying 25 keys.

  • Caesar Cipher encrypts only alphabets; it will not encrypt numbers, spaces, and any other special characters.

  • Caesar cipher text is also in plain English, so it is very for hackers to identify the shift key. 

                                          CSharpCaesarCipher