Skip to content
Educora
University25 min59 / 59

Cryptography and security

Learn symmetric and asymmetric encryption, hash functions, digital signatures, RSA and Diffie–Hellman with small numbers, and common attacks and their defences.

Check yourself
In this lesson you will learn
  • Compare symmetric and asymmetric encryption and explain the key-distribution problem
  • Build RSA keys with small numbers, then encrypt, decrypt and sign
  • Explain the properties of hashes and how to store passwords properly
  • Recognise the main attacks and choose defences against them

When Leyla pays in her banking app, the data passes through dozens of strangers' devices, yet none of them can read or change the amount, and the bank is sure the request really came from Leyla. That is cryptography at work. It serves four goals: confidentiality (others cannot read), integrity (changes are detected), authenticity (we know who sent it) and non-repudiation (the sender cannot later say “it wasn't me”).

Symmetric encryption

C = E(K, M), M = D(K, C)
where:
  • M, Cplaintext and ciphertext
  • Kthe same secret key held by both sides
  • E, Dencryption and decryption algorithms (e.g. AES)

Kerckhoffs's principle: the algorithm may be public; security must rest only on the secrecy of the key.

The modern standard is AES (128-, 192- or 256-bit keys): it is very fast and has hardware support in processors. A 128-bit key has 2¹²⁸ ≈ 3.4 · 10³⁸ possibilities — trying them all is practically impossible. The real problem is different: how do you share the key? Every pair among n people needs its own key — n(n − 1)/2 keys, 499,500 for 1000 users. And the key cannot be sent over an insecure channel the first time.

Asymmetric cryptography: Diffie–Hellman and RSA

In asymmetric cryptography every user has two keys: a public key given to everyone and a private key shown to no one. What is encrypted with the public key can be opened only with the private key. It rests on mathematical operations that are “easy one way, hard to reverse”: multiplying two large primes is easy, but factoring the product is very hard; computing gᵃ mod p is easy, but recovering a (the discrete logarithm) is hard.

A = gᵃ mod p, B = gᵇ mod p, s = Bᵃ mod p = Aᵇ mod p = gᵃᵇ mod p
where:
  • p, ga public prime and base
  • a, bthe two sides' secret numbers
  • sthe shared secret — never sent over the network

Diffie–Hellman key exchange: an eavesdropper sees p, g, A and B, but to get s must solve a discrete logarithm.

Example 1: Diffie–Hellman with small numbers

p = 23, g = 5. Aysel's secret is a = 6, Murad's is b = 15. Find the public numbers and the shared key.

Show solution
Key trick: 5² = 25 ≡ 2 (mod 23).
A = 5⁶ = (5²)³ ≡ 2³ = 8.
B = 5¹⁵ = 5¹² · 5³; 5¹² = (5⁶)² ≡ 64 ≡ 18; 5³ = 125 ≡ 10; 18 · 10 = 180 ≡ 19.
Aysel: s = 19⁶; 19 ≡ −4, (−4)⁶ = 4096 = 23 · 178 + 2 → s = 2.
Murad: s = 8¹⁵ = 2⁴⁵; 2¹¹ = 2048 = 23 · 89 + 1 ≡ 1, so 2⁴⁵ = (2¹¹)⁴ · 2 ≡ 2 ✓.
Both got the same key, yet the number 2 never appeared on the network.
n = p · q, φ(n) = (p − 1)(q − 1), e · d ≡ 1 (mod φ(n)), c = mᵉ mod n, m = cᵈ mod n
where:
  • p, qtwo large secret primes (≈ 1024 bits each in practice)
  • (n, e)the public key
  • dthe private key: the inverse of e modulo φ(n)
  • m, cmessage (0 ≤ m < n) and ciphertext

RSA (Rivest, Shamir, Adleman, 1977). Finding d requires φ(n), and that requires the factors of n — security rests on how hard factoring is.

Example 2: toy RSA

p = 3, q = 11, e = 3. Build the keys, encrypt and decrypt the message m = 4, then sign it and verify the signature.

Show solution
n = 3 · 11 = 33, φ = 2 · 10 = 20. gcd(3, 20) = 1 ✓.
d: 3d ≡ 1 (mod 20) → d = 7 (3 · 7 = 21 = 20 + 1). Public key (33, 3), private key 7.
Encryption: c = 4³ = 64 mod 33 = 31.
Decryption: 31⁷ mod 33; 31 ≡ −2 → (−2)⁷ = −128; 128 = 3 · 33 + 29 → −128 ≡ −29 ≡ 4 ✓.
Signature (with the private key): s = 4⁷ = 16,384 = 33 · 496 + 16 → s = 16.
Verification (with the public key): 16³ = 4096 = 33 · 124 + 4 → 4 = m ✓ — the signature is valid.
Python
p, g = 23, 5
a, b = 6, 15
A, B = pow(g, a, p), pow(g, b, p)
print('DH public:', A, B, '| shared:', pow(B, a, p), pow(A, b, p))

p, q, e = 3, 11, 3
n, phi = p * q, (p - 1) * (q - 1)
d = pow(e, -1, phi)
m = 4
c = pow(m, e, n)
print('RSA n =', n, 'phi =', phi, 'd =', d)
print('encrypt', m, '->', c, '| decrypt ->', pow(c, d, n))
s = pow(m, d, n)
print('signature', s, '| verify ->', pow(s, e, n))
▸ Expected output
DH public: 8 19 | shared: 2 2
RSA n = 33 phi = 20 d = 7
encrypt 4 -> 31 | decrypt -> 4
signature 16 | verify -> 4
In Python, pow(x, k, n) is fast modular exponentiation (by repeated squaring, O(log k) multiplications), and pow(e, -1, phi) finds the modular inverse with the extended Euclidean algorithm. Both examples confirm our hand calculations.

Hash functions and digital signatures

A cryptographic hash (such as SHA-256) turns data of any length into a fixed-length (256-bit) “fingerprint”. Requirements: the same result every time; the data cannot be recovered from the hash (one-wayness); finding two different inputs with the same hash is practically impossible (collision resistance); a one-bit change flips about half the bits of the hash (avalanche effect). A hash is not encryption: it has no key and cannot be reversed.

Python
import hashlib, math

for text in ['Educora', 'educora', 'Educora!']:
    print(text, hashlib.sha256(text.encode()).hexdigest()[:16])

rate = 10 ** 10
for name, n, length in [('8 lowercase', 26, 8), ('12 mixed', 94, 12)]:
    bits = length * math.log2(n)
    seconds = n ** length / rate
    print(name, '|', round(bits, 1), 'bits |', f'{seconds:.3g}', 'seconds')
▸ Expected output
Educora b3dbb4d80128c71a
educora 48ecb7c2aeec01dc
Educora! 836636e98b18e97a
8 lowercase | 37.6 bits | 20.9 seconds
12 mixed | 78.7 bits | 4.76e+13 seconds
Changing the case of one letter or adding one “!” changes the hash completely (the first 16 of 64 hex digits are shown). Below is the arithmetic for an attacker testing 10¹⁰ guesses per second: an 8-letter lowercase password falls in 21 seconds, a random 12-character one from 94 symbols in ≈ 4.76 · 10¹³ seconds — about 1.5 million years.
H = L · log₂ N
where:
  • Hentropy of a random password, bits
  • Lpassword length
  • Nalphabet size (26 lowercase letters, 94 printable characters)

Each extra bit doubles the brute-force effort. The formula holds only for truly random passwords: ones like “Baku2026!” fall quickly to dictionary attacks.

Passwords must never be stored as plain text or with plain SHA-256: the server gives each user a random salt and hashes the password with a deliberately slow function — Argon2, bcrypt, scrypt or PBKDF2. The salt stops equal passwords from producing equal hashes and defeats precomputed tables; the slowness blocks billions of fast guesses. A digital signature works like this: the sender signs the message's hash with their private key, and anyone verifies it with the sender's public key. Who a public key belongs to is confirmed by a certificate — a document signed by a trusted authority (CA).

Attacks and defences

AttackWhat happensDefence
Phishinga fake site or email steals the passwordtwo-factor authentication, checking the address, passkeys
SQL injectioninput text becomes part of the queryparameterised queries, ORMs
XSSa script is injected into someone else's pageescaping output, Content-Security-Policy
Man-in-the-middletraffic is secretly read or alteredTLS, certificate checks
Password guessingbrute-force and dictionary attackslong random passwords, rate limiting, salted slow hashes
Ransomwareencrypts files and demands paymentoffline backups, updates, least privilege
SQL
-- the program glues user input into the query:
--   WHERE email = '<input>'
-- the attacker types:  x' OR '1'='1
SELECT COUNT(*) AS leaked_rows
FROM students
WHERE email = 'x' OR '1'='1';
▸ Expected output
leaked_rows
12
SQL injection on the sample database: the quote “breaks out” of the string, and since OR '1'='1' is always true, all 12 students leak. With a parameterised query (WHERE email = ?) the same input is compared as plain text and returns 0 rows.

Key points

  • Symmetric encryption (AES) is fast but has a key-distribution problem: n people need n(n − 1)/2 keys.
  • Diffie–Hellman creates a shared key without sending it: s = gᵃᵇ mod p.
  • RSA: n = pq, ed ≡ 1 (mod φ), c = mᵉ mod n, m = cᵈ mod n; signature s = mᵈ, verification sᵉ = m.
  • A hash is one-way and collision-resistant; passwords are stored with a salt and a slow hash (Argon2, bcrypt).
  • Parameterised queries stop SQL injection, TLS stops man-in-the-middle attacks; don't roll your own crypto.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
In RSA, p = 5, q = 11 and e = 3. What is the private key d?