Basics of Cryptography
Cryptographic Principles
Cryptography is the study of transforming a message so that only the intended reader can recover it. Every cipher you will meet, from a simple letter shift to the schemes that protect online banking, is built from the same few parts and must satisfy the same mathematical requirement: whatever encryption does, decryption must undo. This lesson sets up that vocabulary and shows it at work in two ciphers built on arithmetic modulo 26.
The parts of a cipher
The readable message is the plaintext m, and the scrambled version sent over an open channel is the ciphertext c. A cipher is a family of encryption functions Ek and decryption functions Dk, one pair for each key k in a set of allowed keys called the key space. The sender computes c=Ek(m), and the receiver, holding the matching key, computes Dk(c). The defining requirement is correctness:
A direct consequence is that each Ek must be one-to-one. If two plaintexts encrypted to the same ciphertext, no decryption function could know which one to return.
The design rule behind modern cryptography is Kerckhoffs's principle: a cipher should stay secure even when everything about it except the key is public. In practice you should assume the attacker knows exactly how E and D work and sees every ciphertext; only k is hidden.
Shift and affine ciphers
To do arithmetic on letters, number them A = 0, B = 1, …, Z = 25 and work in Z26. The shift cipher adds a fixed key k:
Example: With k=3, the letter H (x=7) encrypts to 10, which is K, and subtracting 3 from 10 returns 7.
The affine cipher uses a key pair (a,b) and multiplies before shifting:
Here a−1 is the multiplicative inverse of a modulo 26, the number with a⋅a−1≡1(mod26). It exists exactly when a is coprime to 26, that is, when gcd(a,26)=1. The shift cipher is the special case a=1.
Example: For the key (5,8), the inverse of 5 is 21, because 5⋅21=105=4⋅26+1. The letter C (x=2) encrypts to 5⋅2+8=18, which is S. Decrypting, 21(18−8)=210=8⋅26+2, so D(18)=2 and C comes back.
Cipher | Key | Encryption | Decryption | Condition on the key |
|---|---|---|---|---|
Shift | k | (x+k)mod26 | (y−k)mod26 | none; 26 possible keys |
Affine | (a,b) | (ax+b)mod26 | a−1(y−b)mod26 | gcd(a,26)=1, any b |
Symmetric and public-key schemes
Both ciphers above are symmetric: the receiver's key is the same as the sender's, or is easily computed from it, since a−1 follows from a in a few steps of arithmetic. A symmetric cipher therefore needs the two parties to agree on a key privately before they communicate.
A public-key scheme separates the two keys. The encryption key is published so anyone can send a message, the decryption key stays private, and recovering the private key from the public one must be computationally infeasible. The correctness requirement D(E(m))=m still holds; only the relationship between the keys changes. RSA is the standard example and is studied on its own.
What to remember
- Plaintext m and key k give ciphertext c=Ek(m); correctness means Dk(Ek(m))=m for every key and message.
- Each encryption function must be one-to-one, or decryption is impossible.
- Kerckhoffs's principle: only the key is secret, so assume the method is known.
- Shift: add k modulo 26. Affine: ax+b modulo 26, valid only when gcd(a,26)=1, decrypted with a−1.
- Symmetric schemes share one key; public-key schemes publish the encryption key and keep the decryption key private.
Example: An affine cipher with key (7, 3)
Two friends agree privately to use the affine cipher E(x)=(7x+3)mod26, numbering the letters A = 0, B = 1, …, Z = 25. Work each part before revealing its solution.
a) Show that (7,3) is a valid key and find the decryption multiplier 7−1mod26.
The key is valid when gcd(7,26)=1, so run the Euclidean algorithm:
The last nonzero remainder is 1, so gcd(7,26)=1 and an inverse exists. Back-substitute to write 1 as a combination of 26 and 7:
The coefficient of 7 is ________, so 7⋅(−11)≡1(mod26). Adding 26 to bring it into the range 0 to 25 gives 7−1≡15.
Check: 7 times 15 is ________, which is 4⋅26+1, so the product leaves remainder 1 as required. The decryption function is D(y)=15(y−3)mod26.
b) Encrypt the word HI.
H is x=7. Multiplying and shifting, 7 · 7 + 3 = ________, and 52=2⋅26, so E(7)=0, which is A.
I is x=8. Here 7 · 8 + 3 = ________, and 59−2⋅26=7, so E(8)=7, which is H.
The ciphertext is AH. Notice that the plaintext letter H and the ciphertext letter H are unrelated positions; an affine cipher moves each letter independently.
c) The receiver gets the ciphertext QX. Decrypt it.
Use D(y)=15(y−3)mod26 from part a.
Q is y=16. Then y−3=13, and 15 · 13 = ________. Since 7⋅26=182, 195−182=13, which is N.
X is y=23. Then y−3=20, and 15 · 20 = ________. Since 11⋅26=286, 300−286=14, which is O.
The plaintext is NO. Check by encrypting N: 7⋅13+3=94=3⋅26+16, which is Q, matching the first ciphertext letter.
Two people want to exchange a private message using an affine cipher with the 26-letter alphabet coded as A = 0, B = 1, …, Z = 25 and a shared secret key (a,b), where gcd(a,26)=1. Put the stages of the exchange in the order they must happen, from first to last.
1.
The sender converts the results to letters and transmits the ciphertext.
2.
The sender and receiver agree on the key (a,b) over a secure channel.
3.
The sender converts each plaintext letter to a number x from 0 to 25 and computes y=(ax+b)mod26 using the agreed key.
4.
The receiver converts the recovered numbers to letters to read the plaintext.
5.
The receiver converts each received ciphertext letter to a number y and computes x=a−1(y−b)mod26.
An affine cipher numbers the letters A = 0 through Z = 25 and encrypts each letter by E(x)=(ax+4)mod26. Which choice of the multiplier a gives a key that the intended receiver can always decrypt correctly?