Skip to content

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
Lessons
6
Python scripts
1,695
Lines of code
3 of 5
With review date

Lessons

Read in order, or jump to any topic

  1. 1Asymmetric Encryption (RSA)DraftAsymmetric cryptography uses a key pair: anyone can encrypt or verify with the public key, but only the private key holder can decrypt or sign. It solves the problem of sharing a secret with someone you have never met.CWE-3270 detection, 0 prevention, 1 otherReviewed Oct 11, 2026
  2. 2Digital SignaturesDraftA digital signature proves that data came from the holder of a private key and has not been changed since. Anyone with the public key can check it. It provides integrity and authenticity, and non-repudiation when the key is controlled by one party.T1553.002CWE-3470 detection, 0 prevention, 1 otherReviewed Oct 11, 2026
  3. 3Hashing FunctionsNot reviewedHashing is a one-way cryptographic function that converts input data of any size into a fixed-length output (hash). Unlike encryption, hashing is irreversible - you cannot retrieve the original data from the hash.0 detection, 0 prevention, 2 otherNot yet reviewed
  4. 4Key ManagementDraftKey management is everything that happens to a cryptographic key after you choose an algorithm: generation, storage, distribution, use, rotation, retirement and destruction. Strong encryption with badly handled keys offers little protection.T1552CWE-3210 detection, 0 prevention, 1 otherReviewed Oct 11, 2026
  5. 5SymmetricNo page yetScripts in 05-cryptography/encryption/symmetric. This folder has no documentation page yet.0 detection, 0 prevention, 1 otherNot yet reviewed

Python scripts in this category

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