Table of Contents

https://drive.google.com/file/d/1TIeK1rpQMfTS7lH_6Rf-Vjq5VeziDcQB/view?usp=sharing

Cryptography Full Concepts (Best Practice + System Design View)

This document summarizes cryptography in a practical, backend-engineer-oriented way: - NOT by algorithm only - BUT by security design + system usage


1. Core Security Goals

Cryptography exists to achieve:


2. Cryptographic Building Blocks

These are the 4 fundamental primitives:

Each system is built by combining these.


3. Encryption (Confidentiality)

Goal: Hide data from unauthorized access


3.1 Symmetric Encryption

Same key for encrypt/decrypt

Properties:

  1. Very fast
  2. Used for large data
  3. Requires secure key sharing

3.2 Asymmetric Encryption

Public key + Private key

Properties:

  1. Slow
  2. Used for small data or key exchange

3.3 Best Practice Pattern (IMPORTANT)

Hybrid Encryption:

1. Use Asymmetric crypto to exchange key
2. Use Symmetric crypto to encrypt data

Example:

TLS (HTTPS)

4. Hashing (Integrity)

Goal: Detect if data was changed

Properties:

  1. No key
  2. One-way function
  3. Cannot decrypt

Broken algorithms:

  1. MD5
  2. SHA-1

5. Authentication

Goal: Verify message origin


5.1 Symmetric Authentication

Properties:

  1. Shared secret key
  2. Fast
  3. No non-repudiation

Used in:

  1. JWT HS256
  2. Internal APIs
  3. Webhooks (shared secret)

5.2 Asymmetric Authentication

Digital Signatures:

Properties:

  1. Private key signs
  2. Public key verifies
  3. Provides non-repudiation

Used in:

  1. JWT RS256 / ES256
  2. OAuth2 / OpenID Connect
  3. SSO systems

6. Key Exchange

Goal: Securely establish shared secret

Flow:

  1. Asymmetric crypto establishes shared key
  2. Then symmetric encryption is used

Used in:

  1. TLS handshake
  2. Secure channels

7. PKI (Trust System)

Public Key Infrastructure:

Purpose:

  1. Prove identity of services
  2. Establish trust between systems

Used in:

  1. HTTPS
  2. mTLS
  3. SSO systems

8. Secure Communication Protocols

TLS example flow:

1. Key exchange (ECDH)
2. Certificate validation (PKI)
3. Symmetric encryption (AES-GCM)

9. Password Security

IMPORTANT RULE: Never encrypt passwords.

Use hashing only:

Enhancements:


10. Key Management

Key lifecycle:

Best practices:

  1. Use KMS (AWS KMS, GCP KMS)
  2. Never hardcode secrets
  3. Separate keys per environment

11. JWT (JSON Web Token)

JWT is NOT encryption.

It is:

→ Token format + signature mechanism

Structure:

header.payload.signature

11.1 JWT Categories


11.2 JWT Usage Model

Authentication layer
  ├── Symmetric (HMAC)
  │     └── HS256 JWT
  │
  └── Asymmetric (Signature)
        └── RS256 / ES256 JWT

11.3 Best Practice


12. Cryptography by Design Principle

Modern system design rules:


13. Final Mental Model

Cryptography in real systems:

1. Asymmetric crypto
    → establish trust / exchange key
2. Symmetric crypto
    → encrypt data efficiently
3. Hashing
    → detect changes
4. Authentication
    → prove identity (HMAC / Signature)
5. PKI
    → manage trust between systems
6. TLS
    → combine everything into secure communication

14. One-Line Summary

Symmetric  → speed (data encryption)
Asymmetric → trust (identity + key exchange)
Hashing    → integrity
JWT        → authentication format using above primitives