Caesar Cipher

A substitution cipher that shifts characters by a fixed number of positions.

Algorithm & Math

The cipher replaces each letter in the plaintext with a letter a fixed number of positions down the alphabet.

  • Encryption: E(x) = (x + shift) mod 26
  • Decryption: D(x) = (x - shift) mod 26
View C++ Source Code

Encrypted:

Decrypted:

Monoalphabetic Cipher

Maps each letter of the alphabet to a fixed, alternative letter.

Algorithm & Explanation

Unlike the Caesar cipher which uses a mathematical shift, this cipher creates a strict 1-to-1 mapping between the standard alphabet and a permuted 26-letter key.

View C++ Source Code

Encrypted:

Decrypted:

Cryptanalysis: Frequency Analysis Attack

By counting the most frequent letters in the ciphertext and mapping them to the most frequent letters in the English language (E, T, A, O, I, N...), we can attempt to crack the cipher without the key.

Note: This works best on very long pieces of text!

RSA Algorithm

Public-key cryptography based on factoring large prime numbers.

Algorithm & Math

  • Key Generation: Generate two primes p and q. Compute the modulus n = p × q and the totient φ(n) = (p-1)(q-1).
  • Public Key (e, n): Choose an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1.
  • Private Key (d, n): Compute d as the modular multiplicative inverse of e modulo φ(n).
  • Encryption: C = Me mod n
  • Decryption: M = Cd mod n
View C++ Source Code

Primes: p = , q =

Keys: Public (e, n) = | Private (d, n) =

Encrypted Cipher:

Decrypted Message:

Playfair Cipher

Encrypts pairs of letters (digraphs) using a 5x5 matrix.

Algorithm & Math

Constructs a 5x5 grid using a key (treating 'J' as 'I'). Plaintext is split into pairs. The encryption follows three geometric rules:

  • Same Row: Replace each with the letter to its right (wrapping around).
  • Same Column: Replace each with the letter below it (wrapping around).
  • Rectangle: Form a rectangle with the two letters and swap them with the letters on the opposite horizontal corners.
View C++ Source Code

Generated Matrix

Formatted Text:

Encrypted:

Decrypted:

Hill Cipher

A polyalphabetic substitution cipher based on linear algebra.

Algorithm & Math

Operates on blocks of text using matrix multiplication. For a 2x2 key matrix K and a plaintext vector P:

  • Encryption: C = (P × K) mod 26
  • Decryption: P = (C × K-1) mod 26

The key matrix must be invertible modulo 26 (i.e., gcd(det(K), 26) = 1).

View C++ Source Code

Determinant:

Inverse Matrix mod 26:

Encrypted:

Decrypted: