Caesar Cipher C# complete

Hi I just wanted to share my Caesar Cipher project

I have completed the extra challenges in Q14 but I did not make one distinct method for both the Encrypt() and Decrypt() methods. Instead I bundle them together and made 1 master method for both of them I called Cryptic() where its third param is the key of turning it into Encrypt() method or Decrypt() Method. So if I pass into the third argument a minus number it will become Decrypt() method if I pass in a plus number which is by default inside the method’s third parameter it will stay as a Encrypt() method. Moreover I made sure to monitor for special characters and numbers a user might input with the help off Regex then continue “jump over it” if i find 1.

Long story short here is the full code

using System;
using System.Text.RegularExpressions;

namespace CaesarCipher
{
    class Program
    {
        static void Main(string[] args)
        { // hello => khoor
            char[] alphabet = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

            /*
             challenge on Q14 done inside the Main method and instead of having 2 methods doing nearly the same thing I done 1 method doing both of the two things of encryption or decryptions
             */
            Console.WriteLine("Welcome to the Ceaser Cipher where you got two options.");
            string msgString;
            string result;
            bool game = true;
            while (game)
            {
                Console.Write("Encrypt? or Decrypt? ");
                string choice = Console.ReadLine().ToLower();
                if (choice == "encrypt")
                {
                    Console.Write("Type a secret message: ");
                    msgString = Console.ReadLine().ToLower();
                    result = Cryptic(alphabet, msgString);
                    Console.WriteLine(result);
                    game = false;
                }
                else if (choice == "decrypt")
                {
                    Console.Write("Type the secret: ");
                    msgString = Console.ReadLine().ToLower();
                    result = Cryptic(alphabet, msgString, -3);
                    Console.WriteLine(result);
                    game = false;
                }
                else
                {
                    Console.WriteLine("Please type what we are asking");
                }
            }


        }

        static string Cryptic(char[] theAlphabet, string message, int addOrSubtract = 3)
        {
            // Q14 look for special chars we are using regex
            Regex special = new Regex("[\\s\\d\\\"\\'/!£$%^&*\\(\\)_+\\][\\}\\{#~'@\\;\\:\\/\\.\\,`¬\\?<>]");
            // Q2
            char[] secretMessage = message.ToCharArray();

            // Q3
            char[] encryptedMessage = new char[secretMessage.Length];

            // Q4 - Q9
            for (int i = 0; i < secretMessage.Length; i++)
            {


                char letter = secretMessage[i];

                // Q14 if we have special char we jump over this iteration
                if (special.IsMatch("" + letter)) // quick converting type char to type string by using "" + char
                {
                    continue;
                }

                // if above if statement is not met we continue the work
                int indexAlpha = Array.IndexOf(theAlphabet, letter);
                int tempIndex = indexAlpha + addOrSubtract;
                if (tempIndex < 0)
                {
                    tempIndex = Math.Abs(theAlphabet.Length + tempIndex);
                }
                int newIndex = (tempIndex) % theAlphabet.Length;
                letter = theAlphabet[newIndex];

                encryptedMessage[i] = letter;
            }
            // Q10 - Q13 loads of test from q11 to q13
            string secretWord = String.Join("", encryptedMessage);
            return secretWord;
        }
    }
}
1 Like