Skip to content

Cryptography (CSC363): Historical ciphers

Published:

Shift Cipher

The shift cipher is a simple substitution cipher.

To encrypt a message, every letter of the plaintext is shifted by a fixed amount.

To decrypt it, it is sufficient to shift the letters back by the same amount.

Example: shift by 3

Loading diagram...
Loading diagram...

Example: python implementation

def shift_cipher(text: str, shift: int) -> str:
    result = ""
    for char in text:
        if char.isalpha():
            ascii_offset = 65 if char.isupper() else 97
            result += chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
        else:
            result += char
    return result
cipher_text = shift_cipher("Hello", 3)
print(cipher_text)  # Khoor
plaintext = shift_cipher(cipher_text, -3)
print(plaintext)  # Hello

Multiple shifts

Combining multiple shifts does not increase the security of the cipher.

If fact, it is equivalent to a single shift by the sum of the shifts modulo 26.

Tip

Shifting by 3 and then by 5 is equivalent to a single shift by 8.

Substitution Cipher

The substitution cipher is a more complex cipher that replaces each letter of the plaintext with another letter.

Example

dvozlnv gl gsv zibkgltixksb nlwfov gsv nlwfov droo zlevi gsv kirmzrkovh xmw
gvzsmrjfvh lu hvzfirmt zlnkfgvih xmw mvgdliph rg rh hkorg rmgl gdl kxigh dsviv
gsv urihg rh zlmzvimvw drgs hbnnvgirz zibkgltixksb dsvivxh gsv hvzlmw kxig rh
zlmzvimvw drgs xhbnnvgirz zibkgltixksb xnlmt lgsvi gsrmth gsv nlwfov vckoxrmh gsv
zibkgltixksrz gsvlib rmzofwrmt nxmb zoxhhrzxo xh dvoo xh klkfoxi zrksvih gsv
pmldovwtv droo svok blf rm wvhrtmrmt xmw wvevolkrmt hvzfiv xkkorzxgrlmh xmw hvzfiv
zlnkfgvi mvgdliph gsrh rh dsviv gsvlib nvvgh kixzgrzv
WELCOME TO THE CRYPTOGRAPHY MODULE THE MODULE WILL COVER THE PRINCIPLES AND
TECHNIQUES OF SECURING COMPUTERS AND NETWORKS IT IS SPLIT INTO TWO PARTS WHERE
THE FIRST IS CONCERNED WITH SYMMETRIC CRYPTOGRAPHY WHEREAS THE SECOND PART IS
CONCERNED WITH ASYMMETRIC CRYPTOGRAPHY AMONG OTHER THINGS THE MODULE EXPLAINS THE
CRYPTOGRAPHIC THEORY INCLUDING MANY CLASSICAL AS WELL AS POPULAR CIPHERS THE
KNOWLEDGE WILL HELP YOU IN DESIGNING AND DEVELOPING SECURE APPLICATIONS AND SECURE
COMPUTER NETWORKS THIS IS WHERE THEORY MEETS PRACTICE

Vigenère Cipher

The Vigenère cipher is a substitution cipher that uses a keyword to determine the shift for each letter.

Example: cyphertext BRUEBRSMBSF

Note that the plaintext contains 2 occurrences of 3 identical letters, and that the 1st and the 5th letters in the plaintext are identical. Also, the 3rd letter in the plaintext is C. Moreover, the key itself has 2 identical letters.

What is the key and the plaintext corresponding to the ciphertext?

BRUEBRSMBSF
-_C_-______

Example: python implementation

def vigenere_cipher(text: str, key: str, encrypt: bool) -> str:
    result = ""
    key_length = len(key)
    for i, char in enumerate(text):
        if char.isalpha():
            ascii_offset = 65 if char.isupper() else 97
            shift = ord(key[i % key_length].upper()) - 65
            if not encrypt:
                shift = -shift
            result += chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
        else:
            result += char
    return result