4.5.6.10 - Encryption, Caesar and Vernam ciphers (VB.NET)

4.5.6.10 - Encryption, Caesar and Vernam ciphers (VB.NET)

Learn how a cipher transforms a message, apply Caesar substitution and Vernam XOR, and explain the conditions behind perfect secrecy. VB.NET source tasks are written-only formative companion applications, not required Paper 2 response forms. This route is incomplete: runnable support pending.

Plaintext, ciphertext and keys

Encryption changes a readable message into a form that someone without the necessary secret information should not be able to understand.

Encryption

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

The plaintext is the original data; it need not be English text. A photograph or a sequence of sensor readings can also be plaintext. The ciphertext is the encrypted data. A cipher is the rule or algorithm used for the transformation. A key selects a particular transformation under that rule. Decryption reverses the transformation to recover the plaintext using the appropriate key.

For example, a rule might shift every letter around the alphabet. Knowing that rule is different from knowing whether this message used a shift of 3 or 11. Secure systems should not depend on keeping the algorithm secret: an attacker may know the cipher but should still lack the key needed to recover the message.

An agreed character encoding, such as assigning A the number 0, gives data a representation. It does not make the data secret when everyone can reverse that encoding. Encryption also does not, by itself, prove who sent a message or that it has not been changed. Those are separate security requirements.

Caesar substitution

A Caesar cipher replaces each letter with the letter a fixed number of positions further around the alphabet.

Use A = 0, B = 1, …, Z = 25. If the key is 5, A becomes F and Z wraps around to E. Every occurrence of a given letter receives the same replacement. Spaces can remain unchanged when the stated convention says so.

For a letter represented by pp, a key kk, and ciphertext value cc:

c=(p+k) mod 26,p=(c−k) mod 26.c=(p+k)\bmod 26,\qquad p=(c-k)\bmod 26.

Here mathematical modulo gives a result from 0 to 25.

Encrypting ZEBRA with key 5 gives EJGWF: the positions 25, 4, 1, 17, 0 become 4, 9, 6, 22, 5. To decrypt EJGWF, move five positions backwards. E becomes Z, rather than a character before A.

This VB.NET implementation accepts only uppercase A–Z and spaces, with a key from 0 to 25. It constructs a new string rather than trying to change immutable string characters.

VB.NET
Module CaesarExample
    Function Encrypt(text As String, key As Integer) As String
        Dim result As New System.Text.StringBuilder()
        For Each symbol As Char In text
            If symbol = " "c Then
                result.Append(symbol)
            Else
                Dim position As Integer = AscW(symbol) - AscW("A"c)
                Dim shifted As Integer = (position + key) Mod 26
                result.Append(ChrW(AscW("A"c) + shifted))
            End If
        Next
        Return result.ToString()
    End Function
End Module

AscW obtains a character's code and ChrW makes the character at a code. Subtracting A's code gives the alphabet position. For these non-negative encryption values, VB.NET's Mod produces the required range. During decryption, a negative dividend can produce a negative remainder, so use ((position - key) Mod 26 + 26) Mod 26 to normalise it.

Caesar is easily cracked. There are only 26 possible shifts, including the unchanged shift 0, so an attacker can try every key and recognise a meaningful result. In a longer message, the fixed substitution also preserves letter-frequency patterns and repeated letters. Hiding the shift does not remove those weaknesses.

Vernam XOR and one-time-pad conditions

A Vernam cipher combines each plaintext bit with the corresponding key bit using exclusive OR, or XOR.

XOR gives 1 when its two inputs differ and 0 when they match.

Plaintext bitKey bitCiphertext bit
000
011
101
110

Writing XOR as ⊕\oplus:

c=p⊕k,p=c⊕k.c=p\oplus k,\qquad p=c\oplus k.

Applying the same key twice cancels its effect because k⊕k=0k\oplus k=0.

Continue this lesson.

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

Create free account