Caesar Encoder Algorithm

The Caesar Encoder Algorithm, also known as Caesar cipher or Caesar's code, is one of the oldest and simplest encryption techniques in the history of cryptography. Named after Julius Caesar, who reportedly used it to protect his private correspondence, this substitution cipher involves replacing each letter in the plaintext by a letter some fixed number of positions down or up the alphabet. The key to this algorithm is the specific shift value, which determines how many positions each letter should be shifted. The Caesar cipher is considered a monoalphabetic substitution cipher, as it uses a single, consistent substitution pattern throughout the entire message. Despite its simplicity, the Caesar Encoder Algorithm was effective in its time, primarily due to the widespread illiteracy and lack of knowledge about cryptography. However, modern encryption techniques have far surpassed the Caesar cipher in terms of security and complexity. Nowadays, the Caesar Encoder Algorithm serves as an excellent introduction to the field of cryptography and basic encoding techniques. It is often used in puzzles, games, and educational settings to illustrate the fundamental concepts of encryption and decryption. While it may not be suitable for securing sensitive data in today's digital world, the Caesar Encoder Algorithm remains an important milestone in the history of cryptography.
using System.Text;

namespace Algorithms.Encoders
{
    /// <summary>
    /// Encodes using caesar cypher.
    /// </summary>
    public class CaesarEncoder : IEncoder<int>
    {
        /// <summary>
        /// Encodes text using specified key,
        /// time complexity: O(n),
        /// space complexity: O(n),
        /// where n - text length.
        /// </summary>
        /// <param name="text">Text to be encoded.</param>
        /// <param name="key">Key that will be used to encode the text.</param>
        /// <returns>Encoded text.</returns>
        public string Encode(string text, int key) => Cipher(text, key);

        /// <summary>
        /// Decodes text that was encoded using specified key,
        /// time complexity: O(n),
        /// space complexity: O(n),
        /// where n - text length.
        /// </summary>
        /// <param name="text">Text to be decoded.</param>
        /// <param name="key">Key that was used to encode the text.</param>
        /// <returns>Decoded text.</returns>
        public string Decode(string text, int key) => Cipher(text, -key);

        private static string Cipher(string text, int key)
        {
            var newText = new StringBuilder(text.Length);
            for (var i = 0; i < text.Length; i++)
            {
                if (!char.IsLetter(text[i]))
                {
                    _ = newText.Append(text[i]);
                    continue;
                }

                var letterA = char.IsUpper(text[i]) ? 'A' : 'a';
                var letterZ = char.IsUpper(text[i]) ? 'Z' : 'z';

                var c = text[i] + key;
                c -= c > letterZ ? (26 * (1 + (c - letterZ - 1) / 26)) : 0;
                c += c < letterA ? (26 * (1 + (letterA - c - 1) / 26)) : 0;

                _ = newText.Append((char)c);
            }

            return newText.ToString();
        }
    }
}

LANGUAGE:

DARK MODE: