Security & Cryptography

The security vocabulary every CS graduate is expected to have: the CIA triad, symmetric and asymmetric cryptography, hashing, digital signatures, TLS, and the attack classes that keep recurring.

If you remember nothing else

  • Confidentiality, Integrity, Availability — every security control serves at least one, and controls often trade one against another.
  • Authentication is who you are; authorisation is what you may do. Authentication always comes first.
  • Symmetric encryption is fast but needs a shared key; asymmetric is slow but solves key distribution — real systems use asymmetric to exchange a symmetric key.
  • Hashing is one-way and is not encryption; there is no 'decrypting' a hash.
  • Passwords must be salted and hashed with a deliberately slow function (bcrypt, argon2), never with a fast one like MD5 or SHA-256 alone.
  • A digital signature gives integrity, authentication and non-repudiation — encryption alone gives none of those.

Core principles

CIA triadGuaranteesBroken byProvided by
ConfidentialityOnly authorised parties can read the dataEavesdropping, data breachEncryption, access control
IntegrityData has not been altered undetectablyTampering, corruptionHashes, MACs, digital signatures
AvailabilityThe system is usable when neededDenial of service, hardware failureRedundancy, backups, rate limiting
  • Least privilege — every user and process gets the minimum access needed for its task, and no more. Limits the blast radius of any compromise.
  • Defence in depth — layer independent controls so that no single failure is fatal. A firewall and input validation and least privilege.
  • Fail securely — when a control errors, it must deny rather than allow. An authentication service that is unreachable should reject, not admit.
  • Never rely on obscurity — Kerckhoffs's principle: a system must remain secure even if everything about it except the key is public. Secret algorithms get reverse-engineered; secret keys can be rotated.
  • Non-repudiation — a party cannot credibly deny having performed an action. Only digital signatures provide it, because only the key holder could have produced the signature.

Symmetric and asymmetric cryptography

SymmetricAsymmetric (public key)
KeysOne shared secret keyA key pair — public and private
Encrypt withThe shared keyThe recipient's public key
Decrypt withThe same shared keyThe recipient's private key
SpeedFast — suitable for bulk dataSlow — often 100–1000× slower
Key distributionThe hard problem — how do both parties get the key?Solved — the public key may be published
Keys for n parties
AlgorithmsAES, DES, 3DES, ChaCha20RSA, ECC, Diffie-Hellman, ElGamal
PropertyAchieved by
ConfidentialityEncrypt with the recipient's public key
Authentication & non-repudiationSign with your own private key
BothSign with your private key, then encrypt the result with their public key

Hashing, MACs and signatures

AlgorithmDigestStatus
MD5128 bitsBroken — collisions are trivially constructible
SHA-1160 bitsBroken — practical collisions demonstrated in 2017
SHA-256 / SHA-3256 bitsSecure for integrity; too fast for passwords
bcrypt / scrypt / Argon2VariesCorrect for passwords — deliberately slow and tunable
Plain textStoring a password correctly
WRONG: store(password) plaintext - catastrophic WRONG: store(md5(password)) broken hash, no salt WRONG: store(sha256(password)) unsalted; GPUs try billions/sec BETTER: store(sha256(salt + password), salt) salted, but still fast RIGHT: store(argon2(password, salt, cost)) salted AND deliberately slow Verification never decrypts. It re-computes: hash(supplied_password, stored_salt) == stored_hash ? The cost factor is tunable: raise it as hardware gets faster, so an offline guessing attack stays expensive for decades.
MechanismProvidesRequiresNon-repudiation?
HashIntegrity onlyNothing — anyone can recompute itNo
MAC / HMACIntegrity and authenticationA shared secret keyNo — either party could have produced it
Digital signatureIntegrity, authentication and non-repudiationThe signer's private keyYes — only the key holder could sign

Certificates, PKI and TLS

Public-key cryptography solves key distribution but creates a new problem: how do you know a public key really belongs to the party it claims? The answer is a certificate — a public key plus identity information, signed by a Certificate Authority that both parties already trust.

  1. Client hello

    The client sends the TLS versions and cipher suites it supports, plus a random value.
  2. Server hello and certificate

    The server picks a cipher suite, sends its own random value, and presents its certificate chain.
  3. Certificate validation

    The client verifies the CA's signature, checks the hostname matches, checks the expiry date, and checks revocation. Any failure aborts the connection.
  4. Key agreement

    Both sides derive a shared symmetric session key — in modern TLS via ephemeral Diffie-Hellman, which gives forward secrecy.
  5. Encrypted session

    All further traffic is encrypted symmetrically with that session key.

Attack classes worth naming

AttackMechanismPrimary defence
SQL injectionUser input is concatenated into a query and changes its structureParameterised queries — never string concatenation. Input validation is secondary
XSS (cross-site scripting)Attacker-supplied script is stored or reflected and runs in another user's browserContext-aware output encoding, Content Security Policy, HttpOnly cookies
CSRFA victim's authenticated browser is tricked into issuing an unintended requestAnti-CSRF tokens, SameSite cookies, checking the Origin header
Buffer overflowWriting past a buffer's bounds overwrites the return addressBounds checking, stack canaries, ASLR, non-executable stack, memory-safe languages
Man-in-the-middleAn attacker relays and possibly alters traffic between two partiesTLS with certificate validation; certificate pinning
Replay attackA valid captured message is retransmitted laterNonces, timestamps, sequence numbers
Privilege escalationGaining rights beyond those grantedLeast privilege, patching, careful setuid use
PythonSQL injection, and the only real fix
# VULNERABLE - user input becomes part of the query's STRUCTURE query = "SELECT * FROM users WHERE name = '" + name + "'" # name = "' OR '1'='1" -> WHERE name = '' OR '1'='1' (matches everything) # name = "'; DROP TABLE users; --" (two statements) # SAFE - the query structure is fixed before the data is ever seen. # The driver sends structure and parameters separately, so input can # never be reinterpreted as SQL. cursor.execute("SELECT * FROM users WHERE name = %s", (name,))

Self-check

10 questions on Security & Cryptography · nothing is stored

0/10 answered
Question 1

1.To send a confidential message to Alice, you encrypt it with:

Question 2

2.The purpose of a salt in password storage is to:

Question 3

3.Which mechanism provides non-repudiation?

Question 4

4.The only reliable defence against SQL injection is:

Question 5

5.SHA-256 is a poor choice for hashing passwords because it is:

Question 6

6.Practical systems combine asymmetric and symmetric cryptography because:

Question 7

7.Forward secrecy means that:

Question 8

8.CSRF differs from XSS in that CSRF:

Question 9

9.Kerckhoffs's principle states that a cryptosystem should remain secure even if:

Question 10

10.HTTPS does NOT protect: