Skip to content

Cryptography | Security - Wyatt's Notes

Cryptography is the mathematical science of securing communication and data. It is not a security Solution by itself — it is a tool that, when correctly applied within a secure system, provides Confidentiality, integrity, authentication, and non-repudiation.

PrimitivePurposeExamples
Symmetric encryptionConfidentiality (high-speed)AES, ChaCha20
Asymmetric encryptionConfidentiality (key exchange)RSA, ECIES
Hash functionsIntegrity, password storageSHA-256, SHA-3, bcrypt
Message authenticationIntegrity + authenticityHMAC, Poly1305
Digital signaturesNon-repudiation, authenticityRSA-PSS, ECDSA, EdDSA
Key exchangeSecure shared secret establishmentDiffie-Hellman, ECDH
Random number generationKey material, nonces, salts/dev/urandomCSPRNG

A cryptosystem should be secure even if everything about the system, except the key, is public Knowledge. This means:

  • The algorithm is published and peer-reviewed
  • Security depends only on key secrecy
  • The algorithm works even if the attacker has full knowledge of its implementation

This is why rolling your own crypto is almost always wrong. AES has been studied for decades by Thousands of cryptanalysts. Your custom cipher has been studied by nobody.

Symmetric encryption uses the same key for encryption and decryption. It is fast (orders of Magnitude faster than asymmetric encryption) and is the standard for bulk data encryption.

AES is a block cipher selected by NIST in 2001 (FIPS 197) as the successor to DES. It operates on 128-bit blocks with key sizes of 128, 192, or 256 bits.

ParameterAES-128AES-192AES-256
Key size128 bits192 bits256 bits
Rounds101214
Security128-bit192-bit256-bit
PerformanceFastestModerateSlightly slower

AES is a substitution-permutation network (SPN). Each round applies:

  1. SubBytes: Non-linear byte substitution using an S-box
  2. ShiftRows: Cyclic permutation of bytes within each row
  3. MixColumns: Linear transformation mixing each column
  4. AddRoundKey: XOR with the round key derived from the key schedule

A block cipher operating on 128-bit blocks needs a mode of operation to handle messages longer than One block. The mode determines how blocks are chained together and how ciphertext is produced.

Each block is encrypted independently with the same key.

Block 1 + Key → Ciphertext Block 1
Block 2 + Key → Ciphertext Block 2
Block 3 + Key → Ciphertext Block 3

Do not use ECB. Identical plaintext blocks produce identical ciphertext blocks, revealing Patterns in the data. The classic demonstration is encrypting an image — ECB preserves visual Structure completely.

Each plaintext block is XORed with the previous ciphertext block before encryption. An Initialization vector (IV) is used for the first block.

IV + Block 1 → XOR → Encrypt → Ciphertext Block 1
Ciphertext Block 1 + Block 2 → XOR → Encrypt → Ciphertext Block 2

CBC requires:

  • IV must be unpredictable (random, not a counter). Reusing an IV with the same key is catastrophic.
  • Padding ( PKCS#7) to align plaintext to block boundaries.
  • Decryption is parallelizable; encryption is sequential.

Vulnerability: CBC is vulnerable to padding oracle attacks if the system leaks information about Whether padding is valid. The BEAST attack (2011) exploited CBC in TLS 1.0 where the IV was the last Ciphertext block of the previous record.

CTR turns a block cipher into a stream cipher. A counter value is encrypted to produce a keystream, Which is XORed with the plaintext.

Counter 0 + Key → Encrypt → Keystream Block 0 → XOR → Plaintext Block 0 → Ciphertext Block 0
Counter 1 + Key → Encrypt → Keystream Block 1 → XOR → Plaintext Block 1 → Ciphertext Block 1

CTR properties:

  • No padding required: It is a stream cipher mode, so plaintext can be any length.
  • Fully parallelizable: Both encryption and decryption can be parallelized.
  • Random access: Any block can be decrypted independently.
  • Nonce requirements: The counter value must never repeat for the same key. A nonce (96 bits) combined with a block counter (32 bits) is standard (NIST SP 800-38A).

Vulnerability: CTR provides confidentiality only. It provides no integrity. If an attacker flips A bit in the ciphertext, the corresponding plaintext bit is flipped and the modification is Undetectable. Always combine with a MAC.

GCM combines CTR mode encryption with Galois field authentication, providing both confidentiality And integrity (AEAD — Authenticated Encryption with Associated Data).

Plaintext → CTR Encryption → Ciphertext
Ciphertext + Associated Data → GHASH → Authentication Tag
Output: Ciphertext + Tag

GCM properties:

  • AEAD: Confidentiality + integrity in a single operation.
  • Associated data: Can authenticate metadata (headers, nonces) without encrypting it.
  • Performance: Hardware-accelerated AES-GCM is extremely fast (AES-NI instruction set).
  • Tag length: 128 bits (16 bytes). Shorter tags (96, 64 bits) reduce security margin.

  • Authentication applies cryptographic primitives to verify user identity through password hashing and token generation.
  • Security Fundamentals defines the confidentiality and integrity goals that cryptographic algorithms are designed to achieve.
  • Web Security uses cryptography to protect web communications through TLS and encrypted data storage.