security:cryptography-fundamentals
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| security:cryptography-fundamentals [2026/06/12 11:05] – phong2018 | security:cryptography-fundamentals [2026/06/13 03:22] (current) – phong2018 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Cryptography Fundamentals ====== | + | https:// |
| - | This document introduces the fundamental concepts of cryptography. | + | ====== 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 | ||
| - | ===== Introduction ===== | + | ----- |
| - | When systems communicate over a network, there are three main security goals: | + | ====== 1. Core Security Goals ====== |
| - | * Confidentiality | + | Cryptography exists to achieve: |
| - | * Integrity | + | |
| - | * Authenticity | + | |
| - | Cryptography helps achieve these goals. | + | * Confidentiality |
| + | - Keep data secret | ||
| - | --- | + | * Integrity |
| + | | ||
| - | ====== Security Goals ====== | + | * Authentication |
| + | - Verify who sent data | ||
| - | ===== Confidentiality ===== | + | * Non-Repudiation |
| + | - Sender cannot deny action | ||
| - | Only authorized parties can read the data. | + | ----- |
| - | Example: | + | ====== 2. Cryptographic Building Blocks ====== |
| - | < | + | These are the 4 fundamental primitives: |
| - | Alice sends a password to Bob. | + | |
| - | Alice ---- Internet ---- Bob | + | * Encryption (Hide data) |
| - | ^ | + | * Hashing (Detect changes) |
| - | Attacker </ | + | * Authentication (Prove origin) |
| + | * Key Management (Control trust) | ||
| - | Without protection, the attacker can read the password. | + | Each system is built by combining these. |
| - | Solution: | + | ----- |
| - | < | + | ====== 3. Encryption |
| - | Encryption | + | |
| - | </ | + | |
| - | --- | + | Goal: Hide data from unauthorized access |
| - | ===== Integrity ===== | + | ----- |
| - | Data must not be modified during transmission. | + | ===== 3.1 Symmetric Encryption ===== |
| - | Example: | + | Same key for encrypt/ |
| - | < | + | * AES (standard) |
| - | Original: | + | * ChaCha20 (modern, fast) |
| - | Transfer $100 | + | |
| - | Modified: | + | Properties: |
| - | Transfer $10000 </ | + | - Very fast |
| + | - Used for large data | ||
| + | - Requires secure key sharing | ||
| - | Solution: | + | ----- |
| - | < | + | ===== 3.2 Asymmetric Encryption ===== |
| - | Digital Signatures | + | |
| - | </ | + | |
| - | --- | + | Public key + Private key |
| - | ===== Authenticity ===== | + | * RSA |
| + | * ECC (ECIES) | ||
| - | Verify who actually sent the data. | + | Properties: |
| + | - Slow | ||
| + | - Used for small data or key exchange | ||
| - | Example: | + | ----- |
| - | < | + | ===== 3.3 Best Practice Pattern (IMPORTANT) ===== |
| - | Someone claims to be your bank. | + | |
| - | </ | + | |
| - | How do you know it is really your bank? | + | Hybrid Encryption: |
| - | Solution: | + | 1. Use Asymmetric crypto to exchange key |
| - | + | 2. Use Symmetric crypto to encrypt | |
| - | < | + | |
| - | Digital Signatures | + | |
| - | Certificates | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== What Is Cryptography? | + | |
| - | + | ||
| - | Cryptography is the practice of protecting information. | + | |
| - | + | ||
| - | Main categories: | + | |
| - | + | ||
| - | < | + | |
| - | Cryptography | + | |
| - | │ | + | |
| - | ├── Encryption | + | |
| - | │ | + | |
| - | └── Digital Signatures | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== What Is A Key? ====== | + | |
| - | + | ||
| - | A key is a value used by cryptographic algorithms. | + | |
| - | + | ||
| - | Think of it like a house key: | + | |
| - | + | ||
| - | < | + | |
| - | With key -> Open the door | + | |
| - | Without key -> Cannot open the door | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Encryption ====== | + | |
| - | + | ||
| - | Encryption protects: | + | |
| - | + | ||
| - | < | + | |
| - | Confidentiality | + | |
| - | </ | + | |
| - | + | ||
| - | Goal: | + | |
| - | + | ||
| - | < | + | |
| - | Prevent unauthorized parties from reading | + | |
| - | </ | + | |
| - | + | ||
| - | Process: | + | |
| - | + | ||
| - | < | + | |
| - | Plain Text | + | |
| - | ↓ | + | |
| - | Encrypt | + | |
| - | ↓ | + | |
| - | Cipher Text | + | |
| - | ↓ | + | |
| - | Decrypt | + | |
| - | ↓ | + | |
| - | Plain Text | + | |
| - | </ | + | |
| Example: | Example: | ||
| + | TLS (HTTPS) | ||
| - | < | + | ----- |
| - | Hello World | + | |
| - | ↓ | + | |
| - | A83D91F22C... | + | |
| - | </ | + | |
| - | + | ||
| - | Only someone with the correct key can recover the original message. | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Types of Cryptography ====== | + | |
| - | + | ||
| - | There are two major cryptographic models: | + | |
| - | + | ||
| - | < | + | |
| - | Cryptography | + | |
| - | │ | + | |
| - | ├── Symmetric Cryptography | + | |
| - | │ | + | |
| - | └── Asymmetric Cryptography | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Encryption vs Digital Signature ====== | + | |
| - | + | ||
| - | A common misconception is that encryption and digital signatures work the same way. | + | |
| - | + | ||
| - | In reality: | + | |
| - | + | ||
| - | ^ Capability ^ Symmetric ^ Asymmetric ^ | + | |
| - | | Encryption | Yes | Yes | | + | |
| - | | Digital Signature | No | Yes | | + | |
| - | + | ||
| - | Explanation: | + | |
| - | + | ||
| - | * Encryption can use either Symmetric or Asymmetric cryptography. | + | |
| - | * Digital Signatures require a Public Key and a Private Key, so they use Asymmetric cryptography. | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Symmetric Cryptography ====== | + | |
| - | + | ||
| - | ===== Definition ===== | + | |
| - | + | ||
| - | Symmetric cryptography uses: | + | |
| - | + | ||
| - | < | + | |
| - | ONE SECRET KEY | + | |
| - | </ | + | |
| - | + | ||
| - | for both encryption and decryption. | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Workflow ===== | + | |
| - | + | ||
| - | < | + | |
| - | Secret Key | + | |
| - | ↓ | + | |
| - | Encrypt | + | |
| - | ↓ | + | |
| - | Cipher Text | + | |
| - | ↓ | + | |
| - | Decrypt | + | |
| - | ↓ | + | |
| - | Plain Text | + | |
| - | </ | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | < | + | |
| - | Encrypt(" | + | |
| - | + | ||
| - | Decrypt(ciphertext, | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Advantages ===== | + | |
| - | + | ||
| - | * Fast | + | |
| - | * Efficient | + | |
| - | * Easy to implement | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Disadvantages ===== | + | |
| - | + | ||
| - | The secret key must be shared securely. | + | |
| - | + | ||
| - | If the key is stolen: | + | |
| - | + | ||
| - | < | + | |
| - | Attacker can decrypt everything. | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Common Algorithms ===== | + | |
| - | + | ||
| - | * AES | + | |
| - | * ChaCha20 | + | |
| - | * DES (legacy) | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Asymmetric Cryptography ====== | + | |
| - | + | ||
| - | ===== Definition ===== | + | |
| - | + | ||
| - | Asymmetric cryptography uses: | + | |
| - | + | ||
| - | < | + | |
| - | TWO KEYS | + | |
| - | + | ||
| - | Public Key | + | |
| - | Private Key </ | + | |
| - | + | ||
| - | The keys are mathematically related. | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Public Key ===== | + | |
| - | + | ||
| - | Public key can be shared freely. | + | |
| - | + | ||
| - | Examples: | + | |
| - | + | ||
| - | * Websites | + | |
| - | * Certificates | + | |
| - | * API documentation | + | |
| - | + | ||
| - | Anyone may know the public key. | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Private Key ===== | + | |
| - | + | ||
| - | Private key must remain secret. | + | |
| - | + | ||
| - | Only the owner should possess it. | + | |
| - | + | ||
| - | If leaked: | + | |
| - | + | ||
| - | < | + | |
| - | Security is compromised. | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Asymmetric Encryption ====== | + | |
| - | + | ||
| - | ===== Purpose ===== | + | |
| - | + | ||
| - | Provides: | + | |
| - | + | ||
| - | < | + | |
| - | Confidentiality | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Workflow ===== | + | |
| - | + | ||
| - | < | + | |
| - | Public Key -> Encrypt | + | |
| - | + | ||
| - | Private Key -> Decrypt </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Example ===== | + | |
| - | + | ||
| - | Alice owns: | + | |
| - | + | ||
| - | < | + | |
| - | Public Key | + | |
| - | Private Key | + | |
| - | </ | + | |
| - | + | ||
| - | Bob wants to send a secret message. | + | |
| - | + | ||
| - | Bob: | + | |
| - | + | ||
| - | < | + | |
| - | Encrypt(message, | + | |
| - | </ | + | |
| - | + | ||
| - | Alice: | + | |
| - | + | ||
| - | < | + | |
| - | Decrypt(ciphertext, | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | ===== Result | + | ====== 4. Hashing (Integrity) ====== |
| - | < | + | Goal: Detect if data was changed |
| - | Anyone can encrypt. | + | |
| - | Only Alice can decrypt. </ | + | * SHA-256 |
| + | * SHA-512 | ||
| + | * SHA-3 | ||
| - | --- | + | Properties: |
| + | | ||
| + | | ||
| + | - Cannot decrypt | ||
| - | ====== Digital Signatures ====== | + | Broken algorithms: |
| + | - MD5 | ||
| + | - SHA-1 | ||
| - | Digital signatures provide: | + | ----- |
| - | * Integrity | + | ====== 5. Authentication ====== |
| - | * Authenticity | + | |
| - | --- | + | Goal: Verify message origin |
| - | ===== Purpose ===== | + | ----- |
| - | Answer two questions: | + | ===== 5.1 Symmetric Authentication ===== |
| - | < | + | * HMAC |
| - | Who sent this? | + | * CMAC |
| - | Was this modified? </ | + | Properties: |
| + | - Shared secret key | ||
| + | - Fast | ||
| + | - No non-repudiation | ||
| - | --- | + | Used in: |
| + | | ||
| + | | ||
| + | | ||
| - | ===== Workflow ===== | + | ----- |
| - | < | + | ===== 5.2 Asymmetric Authentication ===== |
| - | Private Key -> Sign | + | |
| - | Public Key -> Verify </ | + | Digital Signatures: |
| - | --- | + | * RSA-PSS |
| + | * ECDSA | ||
| + | * Ed25519 | ||
| - | ===== Example ===== | + | Properties: |
| + | - Private key signs | ||
| + | - Public key verifies | ||
| + | - Provides non-repudiation | ||
| - | Server signs a document. | + | Used in: |
| + | - JWT RS256 / ES256 | ||
| + | - OAuth2 / OpenID Connect | ||
| + | - SSO systems | ||
| - | < | + | ----- |
| - | Document | + | |
| - | ↓ | + | |
| - | Sign with Private Key | + | |
| - | ↓ | + | |
| - | Signed Document | + | |
| - | </ | + | |
| - | Verification: | + | ====== 6. Key Exchange ====== |
| - | < | + | Goal: Securely establish shared secret |
| - | Signed Document | + | |
| - | ↓ | + | |
| - | Verify with Public Key | + | |
| - | ↓ | + | |
| - | Valid / Invalid | + | |
| - | </ | + | |
| - | --- | + | * Diffie-Hellman (DH) |
| + | * Elliptic Curve Diffie-Hellman (ECDH) | ||
| - | ===== Result ===== | + | Flow: |
| + | - Asymmetric crypto establishes shared key | ||
| + | - Then symmetric encryption is used | ||
| - | < | + | Used in: |
| - | Only the owner can sign. | + | - TLS handshake |
| + | - Secure channels | ||
| - | Everyone can verify. </ | + | ----- |
| - | --- | + | ====== 7. PKI (Trust System) ====== |
| - | ====== Encryption vs Digital Signature ====== | + | Public Key Infrastructure: |
| - | ===== Encryption ===== | + | * X.509 Certificates |
| + | * Certificate Authority (CA) | ||
| + | * Certificate Chain | ||
| - | Goal: | + | Purpose: |
| + | - Prove identity of services | ||
| + | - Establish trust between systems | ||
| - | < | + | Used in: |
| - | Hide data | + | - HTTPS |
| - | </ | + | - mTLS |
| + | - SSO systems | ||
| - | Question answered: | + | ----- |
| - | < | + | ====== 8. Secure Communication Protocols ====== |
| - | Can someone read this? | + | |
| - | </ | + | |
| - | Examples: | + | * TLS (HTTPS) |
| + | * SSH | ||
| + | * IPsec | ||
| + | * OpenPGP | ||
| - | < | + | TLS example flow: |
| - | AES | + | 1. Key exchange (ECDH) |
| - | ChaCha20 | + | 2. Certificate validation (PKI) |
| - | RSA Encryption | + | 3. Symmetric encryption (AES-GCM) |
| - | </ | + | |
| - | Workflows: | + | ----- |
| - | Symmetric: | + | ====== 9. Password Security ====== |
| - | < | + | IMPORTANT RULE: |
| - | Secret Key -> Encrypt | + | Never encrypt passwords. |
| - | Secret Key -> Decrypt | + | |
| - | </ | + | |
| - | Asymmetric: | + | Use hashing only: |
| - | < | + | * Argon2 (best) |
| - | Public Key -> Encrypt | + | |
| - | Private Key -> Decrypt | + | * PBKDF2 (legacy) |
| - | </ | + | |
| - | --- | + | Enhancements: |
| + | * Salt | ||
| + | * Pepper | ||
| - | ===== Digital Signature ===== | + | ----- |
| - | Goal: | + | ====== 10. Key Management ====== |
| - | < | + | Key lifecycle: |
| - | Verify authenticity | + | |
| - | Detect tampering | + | |
| - | </ | + | |
| - | Questions answered: | + | * Generation |
| + | * Storage | ||
| + | * Rotation | ||
| + | * Revocation | ||
| + | * Expiration | ||
| - | < | + | Best practices: |
| - | Who sent this? | + | - Use KMS (AWS KMS, GCP KMS) |
| + | - Never hardcode secrets | ||
| + | - Separate keys per environment | ||
| - | Was this modified? </ | + | ----- |
| - | Workflow: | + | ====== 11. JWT (JSON Web Token) ====== |
| - | < | + | JWT is NOT encryption. |
| - | Private Key -> Sign | + | |
| - | Public Key -> Verify </ | + | It is: |
| + | → Token format + signature mechanism | ||
| - | Examples: | + | Structure: |
| - | < | + | header.payload.signature |
| - | JWT RS256 | + | |
| - | JWT ES256 | + | |
| - | SSH Key Authentication | + | |
| - | TLS Certificates | + | |
| - | Git Commit Signing | + | |
| - | Code Signing | + | |
| - | </ | + | |
| - | --- | + | ----- |
| - | ====== Real-World Examples ====== | + | ===== 11.1 JWT Categories |
| - | ^ Technology ^ Encryption ^ Digital Signature ^ | + | * HS256 (Symmetric) |
| - | | HTTPS/TLS | AES, ChaCha20 | RSA, ECDSA, Ed25519 | | + | - Uses HMAC |
| - | | SSH | AES, ChaCha20 | RSA, Ed25519 | | + | - Shared secret |
| - | | JWT HS256 | HMAC (shared secret) | No | | + | - Single system trust |
| - | | JWT RS256 | No | RSA Signature | | + | |
| - | | JWT ES256 | No | ECDSA Signature | | + | |
| - | | PGP/GPG | Yes | Yes | | + | |
| - | --- | + | * RS256 / ES256 (Asymmetric) |
| + | | ||
| + | | ||
| + | | ||
| - | ====== Important Note About JWT ====== | + | ----- |
| - | JWT signatures are often confused with digital signatures. | + | ===== 11.2 JWT Usage Model ===== |
| - | JWT HS256: | + | Authentication layer |
| - | < | + | ├── Symmetric (HMAC) |
| - | Shared Secret -> Sign | + | |
| - | Shared Secret -> Verify | + | │ |
| - | </code> | + | |
| + | └── RS256 / ES256 JWT | ||
| - | Uses HMAC and a shared secret. | + | ----- |
| - | JWT RS256: | + | ===== 11.3 Best Practice ===== |
| - | < | + | * Use HS256: |
| - | Private Key -> Sign | + | - Single backend system |
| - | Public Key | + | - Simple Laravel API |
| - | </ | + | |
| - | Uses a true digital signature. | + | * Use RS256/ |
| + | - Microservices | ||
| + | - SSO (Keycloak, Auth0, OAuth2) | ||
| - | --- | + | ----- |
| - | ====== | + | ====== |
| - | ^ Capability ^ Symmetric ^ Asymmetric ^ | + | Modern system design rules: |
| - | | Encryption | Yes | Yes | | + | |
| - | | Digital Signature | No | Yes | | + | |
| - | < | + | * Never design your own cryptography |
| - | Encryption | + | * Always use standard algorithms |
| - | = | + | * Prefer AEAD (AES-GCM, ChaCha20-Poly1305) |
| - | Hide data | + | * Separate encryption / authentication / signing |
| - | = | + | * Use symmetric for performance |
| - | Confidentiality | + | * 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 | + | ----- |
| - | Verify sender | + | ====== |
| - | + | + | |
| - | Detect tampering | + | |
| - | ================ | + | |
| - | Authenticity + Integrity | + | Cryptography in real systems: |
| - | # Symmetric | + | 1. Asymmetric crypto |
| + | → establish trust / exchange key | ||
| - | One Secret Key | + | 2. Symmetric crypto |
| + | → encrypt data efficiently | ||
| - | # Asymmetric | + | 3. Hashing |
| + | → detect changes | ||
| - | Public Key + Private Key | + | 4. Authentication |
| + | → prove identity (HMAC / Signature) | ||
| - | # Encryption | + | 5. PKI |
| + | → manage trust between systems | ||
| - | Symmetric OR Asymmetric | + | 6. TLS |
| + | → combine everything into secure communication | ||
| - | # Digital Signature | + | ----- |
| - | Asymmetric </ | + | ====== 14. One-Line Summary ====== |
| + | Symmetric | ||
| + | Asymmetric → trust (identity + key exchange) | ||
| + | Hashing | ||
| + | JWT → authentication format using above primitives | ||
security/cryptography-fundamentals.1781262343.txt.gz · Last modified: by phong2018
