csharp caesar cipher code example

Example: encrypption in C# console using mod of 26

namespace CaesarCipher
{
  class Program
  {
    static void Main(string[] args)
    {
      // Determining if user wants to encrypt or decrypt.
      Console.WriteLine("Chose One: Encrypt / Decrypt");
      string encryptDecrypt = Console.ReadLine();
      string lowerEncryptDecrypt = encryptDecrypt.ToLower();
      if (lowerEncryptDecrypt == "encrypt")
      {
        // Taking user input to encrypt and converting to lower.
        Console.WriteLine("Enter a message to encrypt.");
        string unencryptedMessage = Console.ReadLine();
        string lowerUnencryptedMessage = unencryptedMessage.ToLower();
        // Taking user input for cipher value and parsing as int.
        Console.WriteLine("Enter a value for the cipher.");
        int cipherValue = Int16.Parse(Console.ReadLine());
        // Converting user input to array.
        char[] secretMessage = lowerUnencryptedMessage.ToCharArray();
        // Calling Encrypt() with user input and printing result to console.
        Console.WriteLine(Encrypt(secretMessage, cipherValue));
      } else if (lowerEncryptDecrypt == "decrypt")
      {
        // Taking user input to encrypt and converting to lower.
        Console.WriteLine("Enter a message to decrypt.");
        string msgToDecrypt = Console.ReadLine();
        string lowerMsgToDecrypt = msgToDecrypt.ToLower();
        // Taking user input for cipher value and parsing as int.
        Console.WriteLine("Enter a value for the cipher.");
        int cipherValue = Int16.Parse(Console.ReadLine());
        // Converting user input to array.
        char[] secretMessage = lowerMsgToDecrypt.ToCharArray();
        // Calling Decrypt() with user input and printing result to console.
        Console.WriteLine(Decrypt(secretMessage, cipherValue));
      } else
      {
        Console.WriteLine("That is not a valid choice. Please restart.");
      }
    }
    // Creating encryption method.
    static string Encrypt(char[] secretMessage, int cipherValue)
    {
      // Array to reference letter position.
      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'};
      // Empty array to hold encrypted message.
      char[] encryptedMessage = new char[secretMessage.Length];
      // Loop to encrypt each letter (skipping symbols) and add to previously empty array.
      for (int i = 0; i < secretMessage.Length; i++)
      {
        char unencryptedLetter = secretMessage[i];
        if (Char.IsLetter(unencryptedLetter) == true)
        {
          int unencryptedAlphabetPosition = Array.IndexOf(alphabet, unencryptedLetter);
          int encryptedAlphabetPosition = (unencryptedAlphabetPosition + cipherValue) % 26;
          char encryptedLetter = alphabet[encryptedAlphabetPosition];
          encryptedMessage[i] = encryptedLetter;
        } else {
          encryptedMessage[i] = unencryptedLetter;
        }
      }
      // Converting encrypted message array into string and returning outside method.
      string encryptedMessageResult = string.Join("", encryptedMessage);
      return encryptedMessageResult;
    }
    // Creating decryption method.
    static string Decrypt(char[] secretMessage, int cipherValue)
    {
      // Array to reference letter position.
      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'};
      // Empty array to hold decrypted message.
      char[] encryptedMessage = new char[secretMessage.Length];
      // Loop to decrypt each letter (skipping symbols) and add to previously empty array.
      for (int i = 0; i < secretMessage.Length; i++)
      {
        char unencryptedLetter = secretMessage[i];
        if (Char.IsLetter(unencryptedLetter) == true)
        {
          int unencryptedAlphabetPosition = Array.IndexOf(alphabet, unencryptedLetter);
          int encryptedAlphabetPosition = (unencryptedAlphabetPosition + (26 - (cipherValue % 26))) % 26;
          char encryptedLetter = alphabet[encryptedAlphabetPosition];
          encryptedMessage[i] = encryptedLetter;
        } else {
          encryptedMessage[i] = unencryptedLetter;
        }
      }
      // Converting decrypted message array into string and returning outside method.
      string encryptedMessageResult = string.Join("", encryptedMessage);
      return encryptedMessageResult;
    }
  }
}