Monoalphabetic Substitution Cipher in C#

In my previous article, we discuss simple substitution technique called Caesar Cipher where there are only 25 possible keys to encrypt and decrypt the message. In Caesar cipher, a hacker can easily decrypt the message just by trying a maximum of 25 possible keys. Today we discuss Monoalphabetic cipher which is somewhat advanced than Caesar cipher. In Monoalphabetic cipher, there are n! Possible keys for n number elements. For example, if you want to form secret key with three letters, there are 3! = 3*2*1 = 6 keys are possible. That means with whole number alphabets we can create 25! Possible keys which are somewhat difficult for a hacker to try. With this is cipher technique, the hacker should have some knowledge about plain text. In this article, we discuss how to encrypt and decrypt the message by using Monoalphabetic Cipher in C#.

Open Microsoft Visual Studio => Create new Console Application, name it as MonoalphabeticCipherCSharp. Add below code for Encryption and Decryption.

using System; 

namespace MonoalphabeticCipherCSharp

{

    class Program

    {

        static void Main(string[] args)

        {

            string secretKey = "tciqrdmwfueokgnvhsapbxjlzy";             

            Console.WriteLine("Enter your String for Monoalphabetic Encryption:");

            Console.Write("\n"); 

            string sPlainText = Console.ReadLine().ToLower();

            Console.Write("\n"); 

            string sCipherText = MonoalphabeticEncryption(sPlainText, secretKey);

            Console.WriteLine("Your Cipher Text: " + sCipherText);

            Console.Write("\n"); 

            string sDecryptedPlainText = MonoalphabeticDecryption(sCipherText, secretKey);

            Console.WriteLine("Your Plain Text: " + sDecryptedPlainText);

            Console.Read();

        } 

        private static string MonoalphabeticEncryption(string sPlainText, string key)

        {

            char[] cipherTextChars = new char[sPlainText.Length];

            for (int i = 0; i < sPlainText.Length; i++)

            {

                if (char.IsLetter(sPlainText[i]) == false)

                {

                    cipherTextChars[i] = sPlainText[i];

                }

                else

                {

                    int j = sPlainText[i] - 97;

                    cipherTextChars[i] = key[j];

                }

            }

            return new string(cipherTextChars);

        } 

        private static string MonoalphabeticDecryption(string sCipherText, string key)

        {

            char[] plainTextChars = new char[sCipherText.Length];

            for (int i = 0; i < sCipherText.Length; i++)

            {

                if (char.IsLetter(sCipherText[i]) == false)

                {

                    plainTextChars[i] = sCipherText[i];

                }

                else

                {

                    int j = key.IndexOf(sCipherText[i]) + 97;

                    plainTextChars[i] = (char)j;

                }

            }

            return new string(plainTextChars);

        }

    }

} 

As shown above, we are using some random string “tciqrdmwfueokgnvhsapbxjlzy” as a secret key. In Monoalphabetic cipher, we have to use distinct characters to form secret key. Run the application and provide some input, it displays the output as below.

Monoalphabetic Cipher Technique disadvantages:

  1. Even though Monoalphabetic offers number of possible keys compare to Caesar Cipher technique, still hacker can decrypt the encrypted message by trying more number of keys. In this method, maximum possible keys restricted to 26!. With modern hardware systems, we can quickly work 26! Keys.

  2. Monoalphabetic cipher technique cannot encrypt special characters and numbers other than alphabetic letters.

                                                                     MonoalphabeticCipherCSharp.zip