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 23:46] – [Quick Summary] phong2018 | security:cryptography-fundamentals [2026/06/13 03:22] (current) – phong2018 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Cryptography Fundamentals ====== | + | https://drive.google.com/file/d/1TIeK1rpQMfTS7lH_6Rf-Vjq5VeziDcQB/view?usp=sharing |
| - | + | ||
| - | This document introduces the fundamental concepts of cryptography and explains Encryption, HMAC, Digital Signatures, Symmetric Cryptography, | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Introduction ===== | + | |
| - | + | ||
| - | When systems communicate over a network, there are four main security goals: | + | |
| - | + | ||
| - | * Confidentiality | + | |
| - | * Integrity | + | |
| - | * Authentication | + | |
| - | * Authenticity | + | |
| - | + | ||
| - | Cryptography helps achieve these goals. | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Security Goals ====== | + | |
| - | + | ||
| - | ===== Confidentiality ===== | + | |
| - | + | ||
| - | Only authorized parties can read the data. | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | < | + | |
| - | Alice sends a password to Bob. | + | |
| - | + | ||
| - | Alice ---- Internet ---- Bob | + | |
| - | ^ | + | |
| - | | | + | |
| - | Attacker </code> | + | |
| - | + | ||
| - | Without protection, the attacker can read the password. | + | |
| - | + | ||
| - | Solution: | + | |
| - | + | ||
| - | < | + | |
| - | Encryption | + | |
| - | </code> | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Integrity ===== | + | |
| - | + | ||
| - | Data must not be modified during transmission. | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | < | + | |
| - | Original: | + | |
| - | Transfer $100 | + | |
| - | + | ||
| - | Modified: | + | |
| - | Transfer $10000 </ | + | |
| - | + | ||
| - | Solution: | + | |
| - | + | ||
| - | < | + | |
| - | HMAC | + | |
| - | Digital Signatures | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Authentication ===== | + | |
| - | + | ||
| - | Verify who actually sent the data. | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | < | + | |
| - | Someone claims to be your bank. | + | |
| - | </code> | + | |
| - | + | ||
| - | How do you know it is really your bank? | + | |
| - | + | ||
| - | Solutions: | + | |
| - | + | ||
| - | < | + | |
| - | HMAC | + | |
| - | Digital Signatures | + | |
| - | Certificates | + | |
| - | </code> | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Authenticity ===== | + | |
| - | + | ||
| - | Authenticity means that data genuinely comes from the claimed sender. | + | |
| - | + | ||
| - | Authenticity is achieved through authentication mechanisms such as: | + | |
| - | + | ||
| - | * HMAC | + | |
| - | * Digital Signatures | + | |
| - | * Certificates | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== What Is Cryptography? | + | |
| - | + | ||
| - | Cryptography is the practice of protecting information. | + | |
| - | + | ||
| - | Main categories: | + | |
| - | + | ||
| - | < | + | |
| - | Cryptography | + | |
| - | │ | + | |
| - | ├── Symmetric Cryptography | + | |
| - | │ │ | + | |
| - | │ | + | |
| - | │ | + | |
| - | │ │ | + | |
| - | │ | + | |
| - | │ | + | |
| - | │ | + | |
| - | └── Asymmetric Cryptography | + | |
| - | │ | + | |
| - | ├── Encryption | + | |
| - | │ | + | |
| - | │ | + | |
| - | └── Digital Signatures | + | |
| - | └── RSA, ECDSA, EdDSA | + | |
| - | </code> | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== 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 </code> | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Encryption ====== | + | |
| - | + | ||
| - | Encryption protects: | + | |
| - | + | ||
| - | < | + | |
| - | Confidentiality | + | |
| - | </ | + | |
| - | + | ||
| - | Goal: | + | |
| - | + | ||
| - | < | + | |
| - | Prevent unauthorized parties from reading data. | + | |
| - | </ | + | |
| - | + | ||
| - | Process: | + | |
| - | + | ||
| - | < | + | |
| - | Plain Text | + | |
| - | ↓ | + | |
| - | Encrypt | + | |
| - | ↓ | + | |
| - | Cipher Text | + | |
| - | ↓ | + | |
| - | Decrypt | + | |
| - | ↓ | + | |
| - | Plain Text | + | |
| - | </ | + | |
| - | + | ||
| - | Example: | + | |
| - | + | ||
| - | < | + | |
| - | 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 | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Symmetric vs Asymmetric ====== | + | |
| - | + | ||
| - | ^ Capability ^ Symmetric ^ Asymmetric ^ | + | |
| - | | Encryption | Yes | Yes | | + | |
| - | | Integrity Check | Yes | Yes | | + | |
| - | | Authentication | Yes | Yes | | + | |
| - | | Digital Signature | No | Yes | | + | |
| - | + | ||
| - | Explanation: | + | |
| - | + | ||
| - | * Encryption can use either Symmetric or Asymmetric cryptography. | + | |
| - | * Integrity and Authentication can be achieved by HMAC or Digital Signatures. | + | |
| - | * True Digital Signatures require a Public Key and a Private Key. | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== 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) | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== HMAC ====== | + | |
| - | + | ||
| - | ===== Definition ===== | + | |
| - | + | ||
| - | HMAC stands for: | + | |
| - | + | ||
| - | < | + | |
| - | Hash-based Message Authentication Code | + | |
| - | </ | + | |
| - | + | ||
| - | HMAC provides: | + | |
| - | + | ||
| - | * Integrity | + | |
| - | * Authentication | + | |
| - | + | ||
| - | HMAC uses: | + | |
| - | + | ||
| - | < | + | |
| - | ONE SHARED SECRET KEY | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Workflow ===== | + | |
| - | + | ||
| - | < | + | |
| - | Message | + | |
| - | + | + | |
| - | Secret Key | + | |
| - | ↓ | + | |
| - | HMAC | + | |
| - | </ | + | |
| - | + | ||
| - | Verification: | + | |
| - | + | ||
| - | < | + | |
| - | Message | + | |
| - | + | + | |
| - | Secret Key | + | |
| - | ↓ | + | |
| - | Recalculate HMAC | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Result ===== | + | |
| - | + | ||
| - | < | + | |
| - | Detect tampering | + | |
| - | Verify sender knows the secret | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Characteristics ===== | + | |
| - | + | ||
| - | * Symmetric | + | |
| - | * Uses one shared secret | + | |
| - | * Not a true Digital Signature | + | |
| - | * Used by JWT HS256 | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== 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 ===== | + | |
| - | + | ||
| - | < | + | |
| - | Anyone can encrypt. | + | |
| - | + | ||
| - | Only Alice can decrypt. </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Digital Signatures ====== | + | |
| - | + | ||
| - | Digital Signatures provide: | + | |
| - | + | ||
| - | * Integrity | + | |
| - | * Authentication | + | |
| - | * Non-repudiation | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Purpose ===== | + | |
| - | + | ||
| - | Answer three questions: | + | |
| - | + | ||
| - | < | + | |
| - | Who sent this? | + | |
| - | + | ||
| - | Was this modified? | + | |
| - | + | ||
| - | Can the sender deny sending it? </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== 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 / Invalid | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Result ===== | + | |
| - | + | ||
| - | < | + | |
| - | Only the owner can sign. | + | |
| - | + | ||
| - | Everyone can verify. </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== HMAC vs Digital Signature ====== | + | |
| - | + | ||
| - | ^ Feature ^ HMAC ^ Digital Signature ^ | + | |
| - | | Key Type | Shared Secret | Public/ | + | |
| - | | Symmetric | Yes | No | | + | |
| - | | Asymmetric | No | Yes | | + | |
| - | | Integrity | Yes | Yes | | + | |
| - | | Authentication | Yes | Yes | | + | |
| - | | Non-repudiation | No | Yes | | + | |
| - | | Speed | Faster | Slower | | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Encryption vs Digital Signature ====== | + | |
| - | + | ||
| - | ===== Encryption ===== | + | |
| - | + | ||
| - | Goal: | + | |
| - | + | ||
| - | < | + | |
| - | Hide data | + | |
| - | </ | + | |
| - | + | ||
| - | Question answered: | + | |
| - | + | ||
| - | < | + | |
| - | Can someone read this? | + | |
| - | </ | + | |
| - | + | ||
| - | Examples: | + | |
| - | + | ||
| - | < | + | |
| - | AES | + | |
| - | ChaCha20 | + | |
| - | RSA Encryption | + | |
| - | </ | + | |
| - | + | ||
| - | Workflows: | + | |
| - | + | ||
| - | Symmetric: | + | |
| - | + | ||
| - | < | + | |
| - | Secret Key -> Encrypt | + | |
| - | Secret Key -> Decrypt | + | |
| - | </ | + | |
| - | + | ||
| - | Asymmetric: | + | |
| - | + | ||
| - | < | + | |
| - | Public Key -> Encrypt | + | |
| - | Private Key -> Decrypt | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ===== Digital Signature ===== | + | |
| - | + | ||
| - | Goal: | + | |
| - | + | ||
| - | < | + | |
| - | Verify authenticity | + | |
| - | Detect tampering | + | |
| - | </ | + | |
| - | + | ||
| - | Questions answered: | + | |
| - | + | ||
| - | < | + | |
| - | Who sent this? | + | |
| - | + | ||
| - | Was this modified? </ | + | |
| - | + | ||
| - | Workflow: | + | |
| - | + | ||
| - | < | + | |
| - | Private Key -> Sign | + | |
| - | + | ||
| - | Public Key -> Verify </ | + | |
| - | + | ||
| - | Examples: | + | |
| - | + | ||
| - | < | + | |
| - | JWT RS256 | + | |
| - | JWT ES256 | + | |
| - | SSH Key Authentication | + | |
| - | TLS Certificates | + | |
| - | Git Commit Signing | + | |
| - | Code Signing | + | |
| - | </ | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== JWT ====== | + | |
| - | + | ||
| - | JWT stands for: | + | |
| - | + | ||
| - | < | + | |
| - | JSON Web Token | + | |
| - | </ | + | |
| - | + | ||
| - | JWT is a token format: | + | |
| - | + | ||
| - | < | + | |
| - | header.payload.signature | + | |
| - | </ | + | |
| - | + | ||
| - | JWT itself is not encryption. | + | |
| - | + | ||
| - | JWT is usually used for: | + | |
| - | + | ||
| - | * Authentication | + | |
| - | * Integrity verification | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== JWT HS256 ====== | + | |
| - | + | ||
| - | JWT HS256 uses: | + | |
| - | + | ||
| - | < | + | |
| - | HMAC-SHA256 | + | |
| - | </ | + | |
| - | + | ||
| - | Workflow: | + | |
| - | + | ||
| - | < | + | |
| - | Payload | + | |
| - | + | + | |
| - | JWT_SECRET | + | |
| - | ↓ | + | |
| - | HMAC Signature | + | |
| - | </ | + | |
| - | + | ||
| - | Verification: | + | |
| - | + | ||
| - | < | + | |
| - | Payload | + | |
| - | + | + | |
| - | JWT_SECRET | + | |
| - | ↓ | + | |
| - | Verify HMAC | + | |
| - | </ | + | |
| - | + | ||
| - | Characteristics: | + | |
| - | + | ||
| - | * Symmetric | + | |
| - | * Uses one shared secret | + | |
| - | * Integrity | + | |
| - | * Authentication | + | |
| - | * Not a true Digital Signature | + | |
| - | * Default in many Laravel applications | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== JWT RS256 ====== | + | |
| - | + | ||
| - | JWT RS256 uses: | + | |
| - | + | ||
| - | < | + | |
| - | RSA Digital Signature | + | |
| - | </ | + | |
| - | + | ||
| - | Workflow: | + | |
| - | + | ||
| - | < | + | |
| - | Private Key | + | |
| - | ↓ | + | |
| - | Sign JWT | + | |
| - | + | ||
| - | Public Key | + | |
| - | ↓ | + | |
| - | Verify JWT </ | + | |
| - | + | ||
| - | Characteristics: | + | |
| - | + | ||
| - | * Asymmetric | + | |
| - | * Uses Public/ | + | |
| - | * Integrity | + | |
| - | * Authentication | + | |
| - | * True Digital Signature | + | |
| - | * Common in OAuth2 and SSO systems | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ====== Real-World Examples ====== | + | |
| - | + | ||
| - | ^ Technology ^ Encryption ^ Authentication / Signature ^ | + | |
| - | | HTTPS/TLS | AES, ChaCha20 | RSA, ECDSA, Ed25519 | | + | |
| - | | SSH | AES, ChaCha20 | RSA, Ed25519 | | + | |
| - | | JWT HS256 | No | HMAC | | + | |
| - | | JWT RS256 | No | RSA Signature | | + | |
| - | | JWT ES256 | No | ECDSA Signature | | + | |
| - | | PGP/GPG | Yes | Yes | | + | |
| - | + | ||
| - | --- | + | |
| ====== Cryptography Full Concepts (Best Practice + System Design View) ====== | ====== Cryptography Full Concepts (Best Practice + System Design View) ====== | ||
security/cryptography-fundamentals.1781307989.txt.gz · Last modified: by phong2018
