Skip to content
bughra.dev
Go back

Cryptography Basics

Core Concepts

Types of Encryption

Symmetric Encryption

Asymmetric Encryption

Hybrid Encryption

Hash Functions

Public Key Cryptography

Public key cryptography (asymmetric cryptography) uses a pair of mathematically related keys:

Core Mechanics

RSA (Rivest–Shamir–Adleman)

Diffie-Hellman Key Exchange

Elliptic Curve Cryptography (ECC)

Digital Signatures

Public Key Infrastructure (PKI)

Comparison of Key Public Key Algorithms

FeatureRSADiffie-HellmanECC
PurposeEncryption, signaturesKey exchange onlyEncryption, signatures, key exchange
AuthenticationCan provideDoesn’t provide by itselfCan provide
PerformanceSlowestModerateFastest
Key size2048-4096 bits2048+ bits256-384 bits
Quantum resistanceVulnerableVulnerableVulnerable, but requires larger quantum resources
Mathematical basisInteger factorizationDiscrete logarithm problemElliptic curve discrete logarithm problem

Authentication with Salted Passwords

Key Question

How does password verification work with random salts if the salt changes each time?

Solution

  1. The salt is not secret and is stored in plaintext alongside the password hash
  2. During authentication:
    • System retrieves the stored salt for that specific user
    • Combines the entered password with the stored salt
    • Hashes this combination
    • Compares resulting hash with stored hash

Example Flow

Registration:
1. User creates password: "password123"
2. System generates random salt: "8f4e2a9c"
3. System combines: "password1238f4e2a9c"
4. System hashes: hash("password1238f4e2a9c") → "a7f9b23..."
5. Stores in database:
   - Salt: "8f4e2a9c"
   - Hash: "a7f9b23..."

Authentication:
1. User enters: "password123"
2. System retrieves stored salt: "8f4e2a9c"
3. System combines: "password1238f4e2a9c"
4. System hashes: hash("password1238f4e2a9c") → "a7f9b23..."
5. Compares with stored hash
6. Match = authenticated

Security Principles

Best Practices

Recognising Password Hashes

PrefixAlgorithm
$y$yescrypt is a scalable hashing scheme and is the default and recommended choice in new systems
$gy$gost-yescrypt uses the GOST R 34.11-2012 hash function and the yescrypt hashing method
$7$scrypt is a password-based key derivation function
$2b$, $2y$, $2a$, $2x$bcrypt is a hash based on the Blowfish block cipher originally developed for OpenBSD but supported on a recent version of FreeBSD, NetBSD, Solaris 10 and newer, and several Linux distributions
$6$sha512crypt is a hash based on SHA-2 with 512-bit output originally developed for GNU libc and commonly used on (older) Linux systems
$md5SunMD5 is a hash based on the MD5 algorithm originally developed for Solaris
$1$md5crypt is a hash based on the MD5 algorithm originally developed for FreeBSD

Share this post on:

Previous Post
Command Injection
Next Post
Brute Forcing with Hydra