4.5.6.10 - Encryption, Caesar and Vernam ciphers (C#)

4.5.6.10 - Encryption, Caesar and Vernam ciphers (C#)

Learn how keys transform readable messages, apply Caesar and Vernam encryption and decryption, and distinguish perfect secrecy from security based on computational difficulty.

This C# route uses written code with no Run and is incomplete: runnable support pending. The cipher theory and calculations are Paper 2 content. The C# implementations are formative companion applications; writing these native methods is not a required Paper 2 response.

Plaintext, ciphertext and keys

Encryption changes a message into a form that an unauthorised reader should not be able to understand.

Encryption

The transformation of plaintext into ciphertext using a cipher and a key, to conceal the message's meaning.

Plaintext is the original readable message. Ciphertext is its encrypted representation. A cipher is the algorithm that performs the transformation; a key is the value or sequence that selects the particular transformation. Decryption recovers plaintext from ciphertext using the appropriate key and inverse process. The algorithm can be public while the key remains secret.

For example, a Caesar cipher is the rule “shift each alphabet letter”; the key might be “shift by three”. The same plaintext encrypted with a different shift normally produces different ciphertext. Encoding a message as ASCII or binary alone does not make it secret: anyone knowing the encoding can reverse it without a secret key.

Encryption primarily protects confidentiality. Encrypting bytes alone does not necessarily show who sent them or detect an alteration. Those are different requirements, so do not infer authenticity or integrity merely from the presence of ciphertext.

Caesar substitution

A Caesar cipher replaces each letter with a letter a fixed distance further through a circular alphabet.

Use the explicit convention A = 0, B = 1, …, Z = 25, with a shift key kk from 0 to 25. Wrap past Z to A. Here spaces are copied unchanged. These conventions matter: another task can supply a different alphabet or treatment of spaces.

For a letter value pp, encryption gives c=(p+k) mod 26c=(p+k)\bmod 26. Decryption gives p=(c−k) mod 26p=(c-k)\bmod 26, choosing the result from 0 to 25.

With key 3, ZOO becomes CRR: Z has value 25, so (25+3) mod 26=2(25+3)\bmod 26=2, which is C. Each O becomes R. To decrypt GRJ, subtract 3 from each letter to recover DOG. Encryption and decryption therefore shift in opposite directions.

The C# method below accepts only uppercase A–Z and spaces. A char can be subtracted from another char to obtain an integer distance. Adding that distance to 'A' and casting to char converts it back to a letter. The indexed loop processes every position, including spaces.

C#
class Cipher
{
    static string Caesar(string text, int key)
    {
        string result = "";
        for (int i = 0; i < text.Length; i++)
        {
            char symbol = text[i];
            if (symbol == ' ')
                result += symbol;
            else
                result += (char)('A' + (symbol - 'A' + key) % 26);
        }
        return result;
    }
}

Tracing Caesar("ZOO AT", 3) gives "CRR DW". With an empty string, the loop does not execute and the result is empty. Key 0 leaves every letter unchanged. These cases follow from the same method, without additional branches.

In C#, % gives a remainder that can be negative. For decryption with keys 0–25, add 26 before % 26: (symbol - 'A' - key + 26) % 26 stays nonnegative and has the same wrapped alphabet result. If comparing a computed message against a candidate ciphertext, compare corresponding characters and require equal lengths; matching only a prefix is insufficient.

A Caesar cipher is easily cracked. There are only 25 non-identity shifts for this alphabet, so an attacker can try every one and recognise readable text. It also preserves repeated-letter patterns and letter frequencies: an E that is common in the plaintext always becomes the same ciphertext letter. A long secret key description does not enlarge this fixed shift space.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account