Skip to content
Threats Guide

05

Cryptography

Cryptography is the practice and study of techniques for secure communication in the presence of adversaries. This section covers encryption algorithms, hashing functions, digital signatures, and key management practices essential for protecting data.

5 topics, 6 scripts

Read the full category guide

Back to Main

Overview

Cryptography is the practice and study of techniques for secure communication in the presence of adversaries. This section covers encryption algorithms, hashing functions, digital signatures, and key management practices essential for protecting data.

Categories Covered

  1. Symmetric Encryption - Same key for encryption/decryption (planned)
  2. Asymmetric Encryption - Public/private key pairs
  3. Hashing Functions - One-way cryptographic hashes
  4. Digital Signatures - Message authentication and non-repudiation
  5. Key Management - Generation, storage, and rotation

Cryptographic Primitives

TypeAlgorithmsUse Cases
SymmetricAES, DES, 3DES, ChaCha20Data at rest, File encryption
AsymmetricRSA, ECC, Diffie-HellmanKey exchange, Digital signatures
HashingSHA-2, SHA-3, MD5 (insecure)Password storage, Integrity checks
MACHMAC, CMACMessage authentication

Quick Start

# Navigate to encryption examples
cd 05-cryptography/encryption/symmetric/
python aes_example.py --encrypt --file secret.txt --key mykey.key

# Try hashing
cd ../hashing/
python password_hashing.py --hash "MySecurePassword123!"
python integrity_checker.py --verify-file important.doc

Security Warnings (Cryptography Best Practices)

  • 🔐 Never roll your own crypto
    Use established, peer-reviewed cryptographic libraries.

  • 🗝️ Keep keys secure
    Use Hardware Security Modules (HSMs) in production environments.

  • 🧮 Use modern algorithms
    Avoid outdated or broken algorithms such as MD5, SHA1, and DES.

  • 🔄 Implement Perfect Forward Secrecy (PFS)
    Use ephemeral keys to protect past sessions if long-term keys are compromised.

  • 🔁 Regular key rotation
    Change encryption keys periodically according to your security policy.


Best Practice Reminder

  • Prefer well-vetted standards (e.g., AES-256, RSA-2048+, ECC, SHA-256+).
  • Follow industry guidance from NIST and OWASP.
  • Conduct regular security reviews and cryptographic audits.

05-cryptography/encryption/README.md

Encryption

Overview

Encryption transforms readable data (plaintext) into an unreadable format (ciphertext) using an algorithm and key. Only those with the correct key can decrypt and read the data.

Types of Encryption

1. Symmetric Encryption

  • Same key for encryption and decryption
  • Fast and efficient for large data
  • Challenge: Secure key distribution

Algorithms:

  • AES (Advanced Encryption Standard) - Current standard
  • ChaCha20 - Fast stream cipher
  • 3DES - Legacy, avoid if possible

2. Asymmetric Encryption

  • Different keys for encryption (public) and decryption (private)
  • Slower but solves key distribution
  • Used for key exchange and digital signatures

Algorithms:

  • RSA - Most common, based on prime factorization
  • ECC (Elliptic Curve Cryptography) - Smaller keys, same security
  • ElGamal - Based on discrete logarithm

Encryption Comparison

AlgorithmKey SizeSpeedSecurityUse Case
AES-256256 bitsFastExcellentFile/disk encryption
ChaCha20256 bitsVery FastExcellentMobile/embedded
RSA-20482048 bitsSlowGoodKey exchange
ECC-256256 bitsModerateExcellentModern applications

Best Practices

  1. Use authenticated encryption (AES-GCM, ChaCha20-Poly1305)
  2. Generate keys properly - Use CSPRNG
  3. Store keys securely - Never hardcode
  4. Rotate keys regularly
  5. Use different keys for different purposes

Source folder: 05-cryptography