Knowledge / Information Technology / Cryptography basics

Cryptography basics

Information Technology (B.E./B.Tech) · Information security · free study notes for exams, placements and interviews

In short

Cryptography is the science of transforming information so that only intended parties can read it (confidentiality), detect tampering (integrity), confirm the sender (authentication) and prevent later denial (non-repudiation). It is the mathematical core under HTTPS, UPI, Aadhaar e-KYC, DigiLocker, net banking, VPNs and digital signatures on income-tax and GST filings. In Indian universities it appears in Information Security, Cryptography and Network Security and Network Security papers, and it carries marks in GATE CS/IT (number theory, RSA, modes of operation) and UGC-NET. Placement interviews at TCS, Infosys, Wipro, Cognizant, Accenture, Paytm, PhonePe, HDFC/ICICI tech teams and product companies regularly ask symmetric-versus-asymmetric, hashing-versus-encryption, and how TLS and digital signatures work. Legally, the Information Technology Act, 2000 recognises digital/electronic signatures (Sections 3 and 3A) and the Controller of Certifying Authorities (CCA) licenses India's CAs.

Explained

Goals, terminology and classification

Classical ciphers (exam favourites)

Symmetric-key cryptography

Modes of operation and authenticated encryption

Hash functions, MACs and key derivation

Public-key cryptography, signatures and PKI

Practical issues: key management and modern threats

Key points

Formulas and examples

Caesar: C = (P + k) mod 26; P = (C − k) mod 26 — letters as A = 0 … Z = 25
Affine: C = (aP + b) mod 26, with gcd(a, 26) = 1
Hill: C = K·P mod 26, P = K⁻¹·C mod 26 — K is an n×n key matrix, gcd(det K, 26) = 1
Brute-force key search: 2n total keys, average 2n−1 trials for an n-bit key
Symmetric keys for n users: n(n−1)/2; asymmetric: 2n keys (n pairs)
Euler totient: φ(pq) = (p−1)(q−1) for distinct primes p, q
RSA: n = p·q; choose e with gcd(e, φ(n)) = 1; d = e⁻¹ mod φ(n); C = Pᵉ mod n; P = Cd mod n
Diffie-Hellman: A = ga mod p, B = gb mod p, shared K = Ba mod p = Ab mod p = gab mod p
Birthday/collision bound: work ≈ 2n/2 for an n-bit digest; 50% collision probability after ≈1.177·√N samples from N values
HMAC(K, M) = H((K ⊕ opad) ‖ H((K ⊕ ipad) ‖ M))
CBC: Ci = E(K, Pi ⊕ Ci−1), C0 = IV; CTR: Ci = Pi ⊕ E(K, nonce ‖ i)
Number of AES-128 blocks for an L-byte message = ceil(L/16)

Example 1 - RSA key generation, encryption and decryption (standard exam numbers). Let p = 7, q = 11 → n = 77, φ(n) = 6 × 10 = 60. Choose e = 13 (gcd(13, 60) = 1).

Example 2 - Diffie-Hellman shared secret. p = 23, g = 5; Asha picks a = 6, Bikram picks b = 15.

Example 3 - Brute-force time and key length. A machine tries 10¹² keys per second (1 tera-key/s).

Example 4 - Birthday attack on digest size.

Example 5 - Classical cipher and mode arithmetic.

PropertySymmetric (AES)Asymmetric (RSA/ECC)Hash (SHA-256)
Keys1 shared secretPublic + private pairNone (MAC adds a key)
SpeedVery fast (GB/s)Slow (ms per operation)Very fast
Typical size128/256-bit keyRSA 2048-4096, ECC 256256-bit digest
ServicesConfidentiality (+integrity with GCM)Confidentiality, signature, key exchange, non-repudiationIntegrity only
Key distributionHard (n(n−1)/2 keys)Easy (publish public key)Not applicable
ReversibleYesYesNo (one-way)
Main useBulk data, disk, TLS recordsKey exchange, DSC, certificatesPasswords, signatures, blockchain
ModeParallelNeeds paddingIntegrityVerdict
ECBYesYesNoNever use
CBCDecrypt onlyYesNo (add HMAC)Legacy, needs random IV
CTRYesNoNoGood, nonce must be unique
GCMYesNoYes (AEAD)Preferred today
# Correct, library-based usage - never implement AES or RSA yourself
import os, hashlib, hmac
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

key   = AESGCM.generate_key(bit_length=256)   # CSPRNG-backed key
nonce = os.urandom(12)                        # 96-bit, unique per message
ct    = AESGCM(key).encrypt(nonce, b"PAN: ABCDE1234F", b"ctx-v1")
pt    = AESGCM(key).decrypt(nonce, ct, b"ctx-v1")   # raises if tampered

# Webhook/API signature check in constant time (Razorpay-style)
sig = hmac.new(b"secret", b"payload", hashlib.sha256).hexdigest()
ok  = hmac.compare_digest(sig, "received_signature_here")

Where it is used

Interview questions

Common mistakes

Related topics

Information security fundamentals · Network security: firewalls, VPN and IDS · Identity and access management · Web application security (OWASP Top 10) · Cloud security · Digital forensics

Open in the Knowledge reader →Find jobs →