SecLab
Cryptography & SecretsconceptDifficulty 2/521 min

Picking the right primitive, and inventing nothing

Objective: After this lesson you can pick a cryptographic primitive from a business requirement, and spot three signs of home-made cryptography.

V11A04:2025jwt
Step 1 of 4 · read7 min

Start from the requirement, not the algorithm

The most common cryptographic mistake in applications is not choosing a weak algorithm — it is choosing an algorithm before knowing which property you need. "We need to encrypt this" says nothing yet: encrypted against whom? Do you need to detect tampering? Do you need the same input to give the same output?

The table below runs from requirement to primitive, and that is the right direction:

I need to…PrimitiveIn .NET

Store a password | Password hash | Argon2id (see password-storage) | Keep data secret and detect tampering | AEAD | AesGcm, ChaCha20Poly1305 | Prove data was not altered, same party checks | MAC | HMACSHA256 | Prove origin to a different party | Digital signature | ECDsa (P-256), RSA-PSS | Generate tokens, IDs, salts | CSPRNG | RandomNumberGenerator | Derive a key from another key | KDF | HKDF | Compare two secrets | Constant-time compare | CryptographicOperations.FixedTimeEquals |

Two rows deserve their own note because they are where the wrong choice gets made:

AEAD, not "encryption". AES-CBC alone provides confidentiality and no tamper detection — and an attacker who can modify ciphertext can usually flip your logic too. AES-GCM does both in one call. If you see AesCbc in code with no accompanying HMAC, that is a finding.

A MAC and a signature are not interchangeable. HMAC requires both parties to hold the same key, so the verifying party can also produce one — it proves integrity, not origin. A digital signature signs with a private key and verifies with a public one, so the verifier cannot sign. Choose HMAC for "nobody altered this" and a signature for "provably from me".

Three signs of home-made cryptography, each worth stopping on in review:

  1. XOR with a repeating key. This is not encryption; it is encoding with extra steps.
  2. Hashing something and treating the result as secret. A hash has no key, so anyone recomputes it.
  3. A hand-written function called Encrypt longer than five lines. The right primitive is always one call into a standard library; every extra line is a place to be wrong.
View path

Comments

Join the discussion
Sign up to comment

Commenting needs an account with at least one completed lesson. That condition is what keeps this thread worth reading: every point belongs to someone who can be asked back, and reputation accrues over time.

Sign upSign in

You can still read every comment below without an account. Signing in brings you back to this exact spot, not to the top of the page.

Loading comments…