security:cryptography-fundamentals
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| security:cryptography-fundamentals [2026/06/12 10:42] – created phong2018 | security:cryptography-fundamentals [2026/06/13 03:22] (current) – phong2018 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Cryptography Fundamentals ====== | + | https:// |
| - | This document | + | ====== Cryptography Full Concepts (Best Practice + System Design View) ====== |
| + | |||
| + | This document | ||
| + | - NOT by algorithm only | ||
| + | - BUT by security design + system usage | ||
| ----- | ----- | ||
| - | ===== Introduction | + | ====== 1. Core Security Goals ====== |
| - | When systems communicate over a network, there are three major security concerns: | + | Cryptography exists to achieve: |
| * Confidentiality | * Confidentiality | ||
| + | - Keep data secret | ||
| + | |||
| * Integrity | * Integrity | ||
| - | | + | - Detect data modification |
| + | |||
| + | | ||
| + | - Verify who sent data | ||
| - | These concerns are solved using cryptography. | + | * Non-Repudiation |
| + | - Sender cannot deny action | ||
| ----- | ----- | ||
| - | ====== | + | ====== |
| - | ===== Confidentiality ===== | + | These are the 4 fundamental primitives: |
| - | Confidentiality means: | + | * Encryption (Hide data) |
| + | * Hashing (Detect changes) | ||
| + | * Authentication (Prove origin) | ||
| + | * Key Management (Control trust) | ||
| - | < | + | Each system is built by combining these. |
| - | Only authorized parties can read the data. | + | |
| - | </ | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | < | + | |
| - | Alice sends a password to Bob. | + | |
| - | </ | + | |
| - | + | ||
| - | Without protection: | + | |
| - | + | ||
| - | < | + | |
| - | Alice ---- Internet ---- Bob | + | |
| - | ^ | + | |
| - | | + | |
| - | </ | + | |
| - | + | ||
| - | The attacker can read the message. | + | |
| - | + | ||
| - | Solution: | + | |
| - | + | ||
| - | < | + | |
| - | Encryption | + | |
| - | </ | + | |
| ----- | ----- | ||
| - | ===== Integrity | + | ====== 3. Encryption (Confidentiality) ====== |
| - | Integrity means: | + | Goal: Hide data from unauthorized access |
| - | + | ||
| - | < | + | |
| - | Data was not modified during transmission. | + | |
| - | </ | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | Original: | + | |
| - | + | ||
| - | < | + | |
| - | Transfer $100 | + | |
| - | </ | + | |
| - | + | ||
| - | Modified by attacker: | + | |
| - | + | ||
| - | < | + | |
| - | Transfer $10000 | + | |
| - | </ | + | |
| - | + | ||
| - | Solution: | + | |
| - | + | ||
| - | < | + | |
| - | Digital Signatures | + | |
| - | </ | + | |
| ----- | ----- | ||
| - | ===== Authenticity | + | ===== 3.1 Symmetric Encryption |
| - | + | ||
| - | Authenticity means: | + | |
| - | + | ||
| - | < | + | |
| - | Verify who actually sent the message. | + | |
| - | </ | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | < | + | |
| - | Someone claims to be your bank. | + | |
| - | </ | + | |
| - | How do you know it is really your bank? | + | Same key for encrypt/ |
| - | Solution: | + | * AES (standard) |
| + | * ChaCha20 (modern, fast) | ||
| - | < | + | Properties: |
| - | Digital Signatures | + | - Very fast |
| - | Certificates | + | - Used for large data |
| - | </ | + | - Requires secure key sharing |
| ----- | ----- | ||
| - | ====== What Is Cryptography? | + | ===== 3.2 Asymmetric Encryption |
| - | Cryptography is the science of protecting information. | + | Public key + Private key |
| - | Main categories: | + | * RSA |
| + | * ECC (ECIES) | ||
| - | < | + | Properties: |
| - | Cryptography | + | - Slow |
| - | │ | + | - Used for small data or key exchange |
| - | ├── Encryption | + | |
| - | │ | + | |
| - | └── Digital Signatures | + | |
| - | </ | + | |
| ----- | ----- | ||
| - | ====== Encryption ====== | + | ===== 3.3 Best Practice Pattern (IMPORTANT) |
| - | Encryption | + | Hybrid |
| - | < | + | 1. Use Asymmetric crypto to exchange key |
| - | Confidentiality | + | 2. Use Symmetric crypto to encrypt |
| - | </ | + | |
| - | + | ||
| - | Goal: | + | |
| - | + | ||
| - | < | + | |
| - | Prevent unauthorized people from reading | + | |
| - | </ | + | |
| - | + | ||
| - | Process: | + | |
| - | + | ||
| - | < | + | |
| - | Plain Text | + | |
| - | ↓ | + | |
| - | Encrypt | + | |
| - | ↓ | + | |
| - | Cipher Text | + | |
| - | ↓ | + | |
| - | Decrypt | + | |
| - | ↓ | + | |
| - | Plain Text | + | |
| - | </ | + | |
| Example: | Example: | ||
| - | + | TLS (HTTPS) | |
| - | < | + | |
| - | Hello World | + | |
| - | </ | + | |
| - | + | ||
| - | may become: | + | |
| - | + | ||
| - | < | + | |
| - | A83D91F22C... | + | |
| - | </ | + | |
| - | + | ||
| - | Only someone with the correct key can recover the original message. | + | |
| ----- | ----- | ||
| - | ====== | + | ====== |
| - | A key is a secret value used by cryptographic algorithms. | + | Goal: Detect if data was changed |
| - | Example: | + | * SHA-256 |
| + | * SHA-512 | ||
| + | * SHA-3 | ||
| - | < | + | Properties: |
| - | Message: | + | - No key |
| - | Hello | + | - One-way function |
| + | - Cannot decrypt | ||
| - | Key: | + | Broken algorithms: |
| - | abc123 | + | - MD5 |
| - | </ | + | - SHA-1 |
| - | + | ||
| - | Analogy: | + | |
| - | + | ||
| - | < | + | |
| - | House Key | + | |
| - | </ | + | |
| - | + | ||
| - | Without key: | + | |
| - | + | ||
| - | < | + | |
| - | Cannot open the door. | + | |
| - | </ | + | |
| - | + | ||
| - | With key: | + | |
| - | + | ||
| - | < | + | |
| - | Can open the door. | + | |
| - | </ | + | |
| ----- | ----- | ||
| - | ====== | + | ====== |
| - | There are two major cryptographic models. | + | Goal: Verify message origin |
| - | + | ||
| - | < | + | |
| - | Cryptography | + | |
| - | │ | + | |
| - | ├── Symmetric Cryptography | + | |
| - | │ | + | |
| - | └── Asymmetric Cryptography | + | |
| - | </ | + | |
| ----- | ----- | ||
| - | ====== Symmetric | + | ===== 5.1 Symmetric |
| - | + | ||
| - | ===== Definition | + | |
| - | Symmetric cryptography uses: | + | * HMAC |
| + | * CMAC | ||
| - | < | + | Properties: |
| - | ONE SECRET KEY | + | - Shared secret key |
| - | </ | + | - Fast |
| + | - No non-repudiation | ||
| - | for both encryption and decryption. | + | Used in: |
| + | - JWT HS256 | ||
| + | - Internal APIs | ||
| + | - Webhooks (shared secret) | ||
| ----- | ----- | ||
| - | ===== Architecture | + | ===== 5.2 Asymmetric Authentication |
| - | < | + | Digital Signatures: |
| - | | + | |
| - | | | + | |
| - | | + | |
| - | | + | |
| - | Encrypt | + | |
| - | </ | + | |
| - | ----- | + | * RSA-PSS |
| + | * ECDSA | ||
| + | * Ed25519 | ||
| - | ===== Workflow ===== | + | Properties: |
| + | - Private key signs | ||
| + | - Public key verifies | ||
| + | - Provides non-repudiation | ||
| - | Alice and Bob share the same secret key. | + | Used in: |
| - | + | - JWT RS256 / ES256 | |
| - | < | + | - OAuth2 |
| - | Secret Key = abc123 | + | - SSO systems |
| - | </ | + | |
| - | + | ||
| - | Encryption: | + | |
| - | + | ||
| - | < | + | |
| - | Encrypt(" | + | |
| - | </code> | + | |
| - | + | ||
| - | Decryption: | + | |
| - | + | ||
| - | < | + | |
| - | Decrypt(ciphertext, | + | |
| - | </code> | + | |
| - | + | ||
| - | Same key is used for both operations. | + | |
| ----- | ----- | ||
| - | ===== Real-Life Example | + | ====== 6. Key Exchange ====== |
| - | Think about a locked box. | + | Goal: Securely establish shared secret |
| - | < | + | * Diffie-Hellman (DH) |
| - | Key | + | * Elliptic Curve Diffie-Hellman (ECDH) |
| - | ↓ | + | |
| - | Lock Box | + | |
| - | ↓ | + | |
| - | Unlock Box | + | |
| - | </ | + | |
| - | + | ||
| - | The same key locks and unlocks the box. | + | |
| - | + | ||
| - | ----- | + | |
| - | ===== Advantages ===== | + | Flow: |
| + | - Asymmetric crypto establishes shared key | ||
| + | - Then symmetric encryption is used | ||
| - | * Fast | + | Used in: |
| - | | + | |
| - | | + | |
| ----- | ----- | ||
| - | ===== Disadvantages | + | ====== 7. PKI (Trust System) ====== |
| - | The key must be shared securely. | + | Public Key Infrastructure: |
| - | Problem: | + | * X.509 Certificates |
| + | * Certificate Authority (CA) | ||
| + | * Certificate Chain | ||
| - | < | + | Purpose: |
| - | Alice ---- Secret Key ---- Bob | + | - Prove identity of services |
| - | </ | + | |
| - | If attacker obtains the key: | + | Used in: |
| - | + | - HTTPS | |
| - | < | + | - mTLS |
| - | Attacker can decrypt everything. | + | - SSO systems |
| - | </ | + | |
| ----- | ----- | ||
| - | ===== Common Algorithms | + | ====== |
| - | + | ||
| - | * AES | + | |
| - | * ChaCha20 | + | |
| - | * DES (legacy) | + | |
| - | + | ||
| - | ----- | + | |
| - | + | ||
| - | ====== Asymmetric Cryptography ====== | + | |
| - | + | ||
| - | ===== Definition ===== | + | |
| - | + | ||
| - | Asymmetric cryptography uses: | + | |
| - | + | ||
| - | < | + | |
| - | TWO KEYS | + | |
| - | </ | + | |
| - | + | ||
| - | A key pair: | + | |
| - | < | + | * TLS (HTTPS) |
| - | Public Key | + | * SSH |
| - | Private Key | + | * IPsec |
| - | </ | + | * OpenPGP |
| - | These keys are mathematically related. | + | TLS example flow: |
| + | 1. Key exchange (ECDH) | ||
| + | 2. Certificate validation (PKI) | ||
| + | 3. Symmetric encryption (AES-GCM) | ||
| ----- | ----- | ||
| - | ===== Public Key ===== | + | ====== 9. Password Security ====== |
| - | Public key can be shared freely. | + | IMPORTANT RULE: |
| + | Never encrypt passwords. | ||
| - | Example locations: | + | Use hashing only: |
| - | * Websites | + | * Argon2 (best) |
| - | * Certificates | + | * bcrypt (common) |
| - | * API documentation | + | * PBKDF2 (legacy) |
| - | Anyone may know the public key. | + | Enhancements: |
| + | * Salt | ||
| + | * Pepper | ||
| ----- | ----- | ||
| - | ===== Private | + | ====== 10. Key Management ====== |
| - | Private key must remain secret. | + | Key lifecycle: |
| - | Only the owner should possess it. | + | * Generation |
| + | * Storage | ||
| + | * Rotation | ||
| + | * Revocation | ||
| + | * Expiration | ||
| - | If leaked: | + | Best practices: |
| - | + | - Use KMS (AWS KMS, GCP KMS) | |
| - | < | + | - Never hardcode secrets |
| - | Security is compromised. | + | - Separate keys per environment |
| - | </ | + | |
| ----- | ----- | ||
| - | ====== | + | ====== |
| - | ===== Purpose ===== | + | JWT is NOT encryption. |
| - | + | ||
| - | Provides: | + | |
| - | + | ||
| - | < | + | |
| - | Confidentiality | + | |
| - | </ | + | |
| - | + | ||
| - | ----- | + | |
| - | ===== Workflow ===== | + | It is: |
| + | → Token format + signature mechanism | ||
| - | < | + | Structure: |
| - | Public Key → Encrypt | + | |
| - | Private Key → Decrypt | + | header.payload.signature |
| - | </ | + | |
| ----- | ----- | ||
| - | ===== Example | + | ===== 11.1 JWT Categories |
| - | Alice owns: | + | * HS256 (Symmetric) |
| + | - Uses HMAC | ||
| + | - Shared secret | ||
| + | - Single system trust | ||
| - | < | + | * RS256 / ES256 (Asymmetric) |
| - | Public Key | + | - Uses Digital Signature |
| - | Private Key | + | |
| - | </code> | + | - Public key verifies |
| - | + | ||
| - | Bob wants to send a secret message. | + | |
| - | + | ||
| - | Bob: | + | |
| - | + | ||
| - | < | + | |
| - | Encrypt(message, Alice Public Key) | + | |
| - | </ | + | |
| - | + | ||
| - | Alice: | + | |
| - | + | ||
| - | < | + | |
| - | Decrypt(message, | + | |
| - | </ | + | |
| ----- | ----- | ||
| - | ===== Result | + | ===== 11.2 JWT Usage Model ===== |
| - | < | + | Authentication layer |
| - | Anyone can encrypt. | + | |
| - | Only owner can decrypt. | + | ├── Symmetric (HMAC) |
| - | </code> | + | |
| + | │ | ||
| + | └── Asymmetric (Signature) | ||
| + | └── RS256 / ES256 JWT | ||
| ----- | ----- | ||
| - | ====== Digital Signatures ====== | + | ===== 11.3 Best Practice |
| - | Digital signatures solve: | + | * Use HS256: |
| + | - Single backend system | ||
| + | - Simple Laravel API | ||
| - | * Integrity | + | * Use RS256/ |
| - | * Authenticity | + | - Microservices |
| + | - SSO (Keycloak, Auth0, OAuth2) | ||
| ----- | ----- | ||
| - | ===== Workflow | + | ====== |
| - | + | ||
| - | < | + | |
| - | Private Key → Sign | + | |
| - | + | ||
| - | Public Key → Verify | + | |
| - | </ | + | |
| - | + | ||
| - | ----- | + | |
| - | + | ||
| - | ===== Example ===== | + | |
| - | + | ||
| - | Server signs a document. | + | |
| - | + | ||
| - | < | + | |
| - | Document | + | |
| - | ↓ | + | |
| - | Sign with Private Key | + | |
| - | ↓ | + | |
| - | Signed Document | + | |
| - | </ | + | |
| - | + | ||
| - | Verification: | + | |
| - | + | ||
| - | < | + | |
| - | Signed Document | + | |
| - | ↓ | + | |
| - | Verify with Public Key | + | |
| - | ↓ | + | |
| - | Valid or Invalid | + | |
| - | </ | + | |
| - | + | ||
| - | ----- | + | |
| - | + | ||
| - | ===== Result ===== | + | |
| - | + | ||
| - | < | + | |
| - | Only owner can sign. | + | |
| - | + | ||
| - | Everyone can verify. | + | |
| - | </ | + | |
| - | + | ||
| - | ----- | + | |
| - | + | ||
| - | ====== Encryption vs Digital Signature ====== | + | |
| - | + | ||
| - | ===== Encryption | + | |
| - | + | ||
| - | Goal: | + | |
| - | + | ||
| - | < | + | |
| - | Hide data | + | |
| - | </ | + | |
| - | + | ||
| - | Workflow: | + | |
| - | + | ||
| - | < | + | |
| - | Public Key → Encrypt | + | |
| - | + | ||
| - | Private Key → Decrypt | + | |
| - | </ | + | |
| - | Question answered: | + | Modern system design rules: |
| - | < | + | * Never design your own cryptography |
| - | Can someone read this? | + | * Always use standard algorithms |
| - | </code> | + | * Prefer AEAD (AES-GCM, ChaCha20-Poly1305) |
| + | * Separate encryption | ||
| + | * Use symmetric for performance | ||
| + | * Use asymmetric for trust boundaries | ||
| + | * Use PKI for multi-system identity | ||
| + | * Use TLS everywhere | ||
| + | * Hash passwords only (never encrypt) | ||
| + | * Treat keys as production secrets | ||
| ----- | ----- | ||
| - | ===== Digital Signature | + | ====== 13. Final Mental Model ====== |
| - | Goal: | + | Cryptography in real systems: |
| - | < | + | 1. Asymmetric crypto |
| - | Verify authenticity | + | → establish trust / exchange key |
| - | </code> | + | |
| - | Workflow: | + | 2. Symmetric crypto |
| + | → encrypt data efficiently | ||
| - | < | + | 3. Hashing |
| - | Private Key → Sign | + | → detect changes |
| - | Public Key → Verify | + | 4. Authentication |
| - | </code> | + | → prove identity (HMAC / Signature) |
| - | Question answered: | + | 5. PKI |
| + | → manage trust between systems | ||
| - | < | + | 6. TLS |
| - | Did this really come from the owner? | + | → combine everything into secure communication |
| - | </ | + | |
| ----- | ----- | ||
| + | ====== 14. One-Line Summary ====== | ||
| + | Symmetric | ||
| + | Asymmetric → trust (identity + key exchange) | ||
| + | Hashing | ||
| + | JWT → authentication format using above primitives | ||
security/cryptography-fundamentals.1781260932.txt.gz · Last modified: by phong2018
