hacking-tutorial

Encryption Crash Course

[!NOTE] Encryption is the process of converting plaintext into ciphertext to protect sensitive information from unauthorized access. It is a fundamental aspect of cybersecurity and is used to secure data in transit and at rest.

Symmetric Encryption Asymmetric Encryption
- Uses a single key for both encryption and decryption. - Faster than asymmetric encryption. - Examples: AES, DES, RC4. - Uses a pair of keys (public and private) for encryption and decryption. - Slower than symmetric encryption. - Examples: RSA, ECC, DSA.
- Key distribution can be a challenge since the same key must be shared securely between parties. - Suitable for encrypting large amounts of data. - Key distribution is easier since the public key can be shared openly, while the private key remains secure. - Often used for encrypting small amounts of data, such as digital signatures and key exchange.
- Commonly used for encrypting files, databases, and communication channels. - Commonly used for secure communication, digital signatures, and key exchange.
- Examples of symmetric encryption algorithms include AES (Advanced Encryption Standard), DES (Data Encryption Standard), and RC4. - Examples of asymmetric encryption algorithms include RSA (Rivest-Shamir-Adleman), ECC (Elliptic Curve Cryptography), and DSA (Digital Signature Algorithm).
- Symmetric encryption is generally faster than asymmetric encryption, making it suitable for encrypting large amounts of data. - Asymmetric encryption is generally slower than symmetric encryption, making it more suitable for encrypting small amounts of data, such as digital signatures and key exchange.

Symmetric Encryption (AES)

sequenceDiagram
    participant Sender
    participant Receiver

    Sender->>Sender: Generate Secret Key
    Sender->>Sender: Encrypt Plaintext with Secret Key
    Sender->>Receiver: Send Ciphertext
    Receiver->>Receiver: Decrypt Ciphertext with Secret Key
    Receiver->>Receiver: Obtain Original Plaintext

Symmetric encryption involves the following steps:

  1. Key Generation: A secret key is generated. This key must be kept confidential between the communicating parties.
  2. Encryption: The sender uses the secret key and an encryption algorithm (such as AES or DES) to convert the plaintext into ciphertext.
  3. Transmission: The ciphertext is sent to the receiver over the communication channel.
  4. Decryption: The receiver, who also possesses the same secret key, uses it with the decryption algorithm to convert the ciphertext back into the original plaintext.

Note: The security of symmetric encryption relies entirely on keeping the key secret. If the key is intercepted or leaked, the encrypted data can be easily decrypted.

Block Cipher Modes of Operation

[!NOTE] AES itself only knows how to encrypt one fixed-size block of data (16 bytes) at a time. A mode of operation is the recipe for stitching many blocks together to encrypt a real message of any length. Picking the wrong mode can leak information even though the underlying cipher (AES) is perfectly secure.

[!TIP] Rule of thumb: prefer an AEAD (Authenticated Encryption with Associated Data) mode like AES-GCM or ChaCha20-Poly1305 over plain CBC or CTR. AEAD modes detect tampering automatically instead of relying on you to add a separate integrity check.

Asymmetric Encryption (RSA)

[!NOTE] Asymmetric encryption, also known as public-key cryptography, is a type of encryption that uses a pair of keys: a public key for encryption and a private key for decryption. This allows for secure communication without the need for the sender and receiver to share a secret key beforehand.

sequenceDiagram
    participant Sender
    participant Receiver

    Note right of Receiver: 1. Key Generation
    Receiver->>Receiver: Generates Key Pair (Public & Private Keys)
    
    Note over Sender, Receiver: 2. Key Exchange
    Receiver->>Sender: Sends Public Key (Open Channel)
    
    Note left of Sender: 3. Encryption
    Sender->>Sender: Encrypts Plaintext using Receiver's Public Key
    
    Note over Sender, Receiver: 4. Transmission
    Sender->>Receiver: Sends Ciphertext (Potentially Insecure Channel)
    
    Note right of Receiver: 5. Decryption
    Receiver->>Receiver: Decrypts Ciphertext using Private Key
    Receiver->>Receiver: Obtains Original Plaintext

[!IMPORTANT] Note: The security of asymmetric encryption relies on the difficulty of certain mathematical problems, such as factoring large integers (in the case of RSA) or solving the discrete logarithm problem (in the case of ECC). As long as these problems remain computationally infeasible to solve, asymmetric encryption can provide strong security.

[!IMPORTANT]

Advantages of Asymmetric Encryption:

Diffie-Hellman Key Exchange

[!NOTE] Diffie-Hellman (DH) is not an encryption algorithm at all — it’s a way for two parties to agree on a shared secret over a public channel that an eavesdropper, watching every message, still cannot compute. That shared secret then becomes the key for a symmetric algorithm like AES.

Open the interactive diagram — exportable as PNG/PDF.

Hash Functions

[!NOTE] A hash function is a mathematical function that takes an input (or “message”) and produces a fixed-size string of bytes, typically a digest that is unique to the input. Hash functions are commonly used in cryptography for various purposes, including data integrity verification, password hashing, and digital signatures.

sequenceDiagram
    participant User
    participant HashFunction
    participant Database
    User->>HashFunction: Input Data (e.g., Password)
    HashFunction->>HashFunction: Compute Hash Digest
    HashFunction->>Database: Store Hash Digest
    User->>Database: Login Attempt with Password
    Database->>HashFunction: Retrieve Stored Hash Digest
    HashFunction->>HashFunction: Compute Hash Digest of Login Attempt
    HashFunction->>Database: Compare Hash Digests
    Database->>User: Authentication Result (Success/Failure)

HMAC (Hash-Based Message Authentication Code)

[!NOTE] A plain hash proves data hasn’t changed, but it doesn’t prove who sent it — anyone can recompute a SHA-256 hash. An HMAC fixes this by mixing a shared secret key into the hashing process, so only someone who knows the key could have produced that specific hash.

[!IMPORTANT] HMAC uses a shared secret (symmetric), while digital signatures use a private/public key pair (asymmetric). That’s the key distinction: HMAC is faster but both sides must already trust each other with the same key; digital signatures are slower but let anyone with the public key verify authenticity, with no shared secret required.

Key Derivation Functions & Password Hashing

[!NOTE] Generic hash functions like SHA-256 are built to be fast — great for checksums, terrible for passwords. A GPU can try billions of SHA-256 guesses per second. Key Derivation Functions (KDFs) are deliberately slow and memory-hungry hash functions designed specifically to make password cracking expensive.

[!TIP] Rule of thumb for new projects: use Argon2id if available, otherwise bcrypt. Never store passwords with a plain hash function like SHA-256 or MD5, even with a salt — they’re simply too fast to brute-force at scale.

Digital Signatures

[!IMPORTANT] Digital signatures are a cryptographic mechanism used to verify the authenticity and integrity of digital messages or documents. They provide a way to ensure that a message has not been altered and that it was indeed sent by the claimed sender.

One-Time Pad

[!NOTE] The One-Time Pad (OTP) is the only encryption scheme ever mathematically proven to be unbreakable — not just “hard to break with today’s computers,” but unbreakable even with infinite computing power. It’s also, in practice, almost never used. Understanding why is a great lesson in the gap between theoretical and practical security.

Public Key Infrastructure (PKI) & Certificates

[!NOTE] Asymmetric encryption solves how to encrypt without a shared secret, but it leaves one big question open: when you receive someone’s public key, how do you know it actually belongs to them and not to an attacker impersonating them? PKI is the trust system that answers that question — it’s what makes the padlock icon in your browser meaningful.

Open the interactive diagram — exportable as PNG/PDF.

[!TIP] This is the missing piece that connects everything in this lesson: Diffie-Hellman establishes a shared secret, AES/ChaCha20 encrypts the data with it, HMAC/AEAD modes check integrity, and PKI/certificates confirm you negotiated that secret with the right party in the first place, not an attacker. TLS, covered next, wires all of these pieces together into one protocol.

SSL(Secure Sockets Layer) & TLS(Transport Layer Security)

[!IMPORTANT] SSL stands for Secure Sockets Layer, and TLS stands for Transport Layer Security. Both SSL and TLS are cryptographic protocols designed to provide secure communication over a computer network. TLS is the successor to SSL and is more secure and efficient than its predecessor.

[!NOTE] The best Authentication & Key Exchanging Algorithm to use are ECDHE (Elliptic Curve Diffie-Hellman Ephemeral) for key exchange and ECDSA (Elliptic Curve Digital Signature Algorithm) for authentication. These algorithms provide strong security while also being efficient in terms of performance. ECDHE allows for perfect forward secrecy, which means that even if the server’s private key is compromised in the future, past communications remain secure. ECDSA provides a secure method for verifying the identity of the communicating parties without the need for a trusted third party.

[!WARNING] But the problem is you don’t always get the choice. A server will support only certain authentication * key exchange algorithms only.

Post-Quantum Cryptography

[!WARNING] Everything asymmetric covered in this lesson — RSA, Diffie-Hellman, ECDHE, ECDSA — relies on math problems (factoring large integers, discrete logarithms) that are hard for classical computers. A sufficiently powerful quantum computer running Shor’s Algorithm could solve those same problems efficiently, breaking all of them at once.

[!TIP] You don’t need to change anything in your own projects today, but it’s worth knowing the direction the industry is moving: symmetric crypto (AES-256, SHA-256/384) is already considered quantum-resistant enough; asymmetric crypto is in the middle of a multi-year migration to lattice-based algorithms.