Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Symmetric and asymmetric cryptography for blockchain

Clear, exam-focused notes on symmetric and asymmetric cryptography for blockchain, explaining definitions, core theorems (including Shannon's perfect secrecy), RSA and Diffie–Hellman math, worked examples, common mistakes and exam question types.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Cryptography provides confidentiality, integrity and authentication used throughout blockchain systems: symmetric cryptography for fast encryption and message authentication (e.g. encrypting keys off-chain, MACs), and asymmetric cryptography for identity, key exchange, and digital signatures (public-key pairs used in transactions and wallets). An engineer uses these when designing wallets, securing node communication, implementing transaction signing, and understanding attack surfaces.

Key ideas

Start with definitions (each bolded term is defined before use):

  • Cryptography: the study and practice of techniques for secure communication in the presence of adversaries.
  • Plaintext: the original message that must be kept secret or protected.
  • Ciphertext: the transformed (encrypted) form of plaintext.
  • Key: a value (bit string) that parameterises an encryption or decryption algorithm; in symmetric cryptography the key is secret and shared, while in asymmetric cryptography the public key is openly distributed and the private key is kept secret.
  • Encryption function E_k(m): maps plaintext m to ciphertext c using key k.
  • Decryption function D_k(c): recovers plaintext m from ciphertext c with key k. The correctness requirement is D_k(E_k(m)) = m.

Symmetric vs asymmetric

  • Symmetric cryptography (also secret-key cryptography): the same key (or trivially related keys) is used for both encryption and decryption. Example algorithms: AES, 3DES. Assumption: secure channel or secure method to share the key initially.
  • Asymmetric cryptography (also public-key cryptography): uses a key pair (public key, private key) where the public key is openly distributed and the private key is kept secret. Encryption with public key is inverted by private key (or signatures are created with private key and verified with public key). Example systems: RSA, Diffie–Hellman (key exchange), and elliptic-curve schemes (covered in the next topic).

Security notions and exact definitions

  • Perfect secrecy (Shannon, 1949): A cipher has perfect secrecy iff for all messages m and ciphertexts c, P(M = m | C = c) = P(M = m). Intuition: observing ciphertext gives no information about plaintext.
    • One precise statement: For random variables M (message), K (key), C (ciphertext) with C = E_K(M), perfect secrecy holds if and only if for all m and c with P(C=c) > 0, P(M=m | C=c) = P(M=m).
    • Assumptions for perfect secrecy: key K must be uniformly random, at least as long as the message, never reused, and independent of M (One-Time Pad meets this requirement).

Derivation for One-Time Pad (OTP) perfect secrecy (sketch for exams):

  1. Define OTP: key k is uniformly random bitstring same length as message m; ciphertext c = m XOR k.
  2. For any fixed m and c there is exactly one key k = m XOR c that produces c from m.
  3. Since keys are uniform and independent of messages, P(C=c | M=m) = 1/|K| independent of m; by Bayes’ rule this implies P(M=m | C=c) = P(M=m).
  • Computational security: A scheme is computationally secure if breaking it requires time/resources beyond feasible limits — e.g., indistinguishability under chosen-plaintext attack (IND-CPA) means no polynomial-time adversary can distinguish encryptions of two chosen messages with non-negligible advantage; for digital signatures, the standard notion is existential unforgeability under chosen-message attack (EUF-CMA).

Public-key primitives and relations

  • RSA: security assumption: factorization of large semiprimes (product of two large primes) is computationally hard (more precisely, security relates to the RSA problem; factoring hardness suffices but is not known equivalent). Key generation derives modulus n = p·q, public exponent e, private exponent d with e·d ≡ 1 (mod φ(n)), where φ(n) = (p-1)(q-1). In practice, RSA encryption must use randomized padding (e.g., RSA-OAEP) and RSA signatures must use provable padding (e.g., RSA-PSS); textbook RSA without padding is not IND-CPA secure.
  • Diffie–Hellman (DH): a key-exchange protocol built on modular exponentiation in a cyclic group. Security assumption: discrete logarithm problem is hard in the chosen group (security is often formalized via the Computational/Decisional Diffie–Hellman assumptions). Use in blockchain: establishing shared secrets, some hybrid encryption patterns; in practice use ephemeral DH (e.g., ECDHE) to provide forward secrecy.
  • Kerckhoffs's principle (definition): A cryptosystem should be secure even if everything about the system, except the key, is public knowledge. Assume only keys are secret.

Diagrams to draw (describe exactly):

  1. Symmetric encryption diagram: draw two boxes labelled "Sender" and "Receiver". Draw an arrow from Sender to Receiver labelled "Ciphertext c". Above arrow write "E_k(m)". Beside boxes write that both Sender and Receiver share key k. Label a side note: "Key distribution problem: how to share k securely?"
  2. Asymmetric encryption diagram: draw Sender (left), Receiver (right). Under Receiver write "private key d (secret)" and above write "public key e (published)". Draw arrow Sender->Receiver labelled "E_e(m) = c" and arrow Receiver applies D_d(c) to get m. Indicate public-key directory or blockchain used to publish keys.

Practical interplay in blockchains

  • In practice, blockchains use asymmetric keys for identity (wallet public/private keys), and symmetric cryptography or MACs for fast encryption/HMAC for communication. Hybrid encryption commonly: encrypt data with a symmetric key, encrypt that symmetric key with the recipient's public key for transport. On-chain transaction data is generally public; encryption protects off-chain data, while digital signatures (e.g., ECDSA/EdDSA) authorize transactions. Prefer AEAD schemes (e.g., AES-GCM, ChaCha20-Poly1305) or an encrypt-then-MAC construction, and never reuse nonces/IVs.

Assumptions frequently used in exam derivations

  • Primes in RSA are large and secret; arithmetic is modulo n.
  • For DH, group generator g and prime p are public; private exponents are random in [1, p-2]. In practice, choose g to generate a large prime-order subgroup of Z_p* (or use elliptic-curve groups), pick exponents uniformly modulo the subgroup order q, and validate public keys to avoid small-subgroup attacks.

Formulas

E_k(m) = c — encryption of message m with key k produces ciphertext c. m, k, c are bitstrings; applies to both symmetric and asymmetric families when k denotes key material. D_k(c) = m — decryption recovers m from ciphertext c with key k. n = p · q — RSA modulus where p and q are primes; unit: dimensionless integer; used in RSA key generation. φ(n) = (p − 1) · (q − 1) — Euler's totient for RSA modulus n when n = p·q. e · d ≡ 1 (mod φ(n)) — RSA key relation: e is public exponent, d is private exponent; used for correctness of RSA decryption. c ≡ m^e (mod n) — RSA encryption of message m (as an integer, with 0 ≤ m < n; in practice m is an encoded/padded block) with public exponent e and modulus n. m ≡ c^d (mod n) — RSA decryption using private exponent d. A = g^a mod p, B = g^b mod p — Diffie–Hellman public values where g is generator, p is prime; a,b are private exponents. s = B^a mod p = A^b mod p — shared secret computed by both parties in DH. Keyspace size for k-bit key = 2^k — number of possible keys for key length k bits.

Worked examples

Example 1 — Symmetric-key brute force time (numerical) Problem: A system uses a 128-bit symmetric key. If an attacker can test 10^9 keys per second, estimate average years to find key by brute force. Steps:

  1. Keyspace = 2^128 ≈ 3.4028237 × 10^38.
  2. Average attempts to find key ~ half the keyspace: 2^127 ≈ 1.7014119 × 10^38 attempts.
  3. Time in seconds = 2^127 / 10^9 ≈ 1.7014119 × 10^29 s.
  4. Seconds per year ≈ 31,536,000 (365·24·3600) ≈ 3.1536 × 10^7 s.
  5. Time in years = (1.7014119 × 10^29) / (3.1536 × 10^7) ≈ 5.396 × 10^21 years. Answer: ≈ 5.4 × 10^21 years (average) — shows 128-bit keys resist brute force with realistic compute.

Example 2 — RSA demonstration (small textbook numbers) Given p = 61, q = 53.

  1. n = p · q = 61 · 53 = 3233.
  2. φ(n) = (61 − 1)(53 − 1) = 60 · 52 = 3120.
  3. Choose e = 17 (coprime to 3120). Compute d such that e·d ≡ 1 (mod 3120): d = 2753.
  4. Encrypt m = 65: c ≡ 65^17 mod 3233 = 2790 (textbook result).
  5. Decrypt c: m ≡ 2790^2753 mod 3233 = 65 (recovers message). Answer: ciphertext c = 2790; decrypted m = 65. Note: in exams show modular exponentiation steps or use repeated-squaring tables; show correctness by verifying e·d ≡ 1 (mod φ(n)).

Example 3 — Diffie–Hellman key exchange trace Choose prime p = 23, generator g = 5.

  1. Alice picks a = 6, computes A = 5^6 mod 23 = 8.
  2. Bob picks b = 15, computes B = 5^15 mod 23 = 19.
  3. Alice computes shared secret s = B^a mod 23 = 19^6 mod 23 = 2.
  4. Bob computes s = A^b mod 23 = 8^15 mod 23 = 2. Answer: shared secret s = 2.

Common mistakes

  • For OTP: stating OTP is secure without specifying key must be random, same length as message, and never reused.
  • In Diffie–Hellman: failing to check that g is a generator of a large-order subgroup (small primes/g can make it insecure).
  • Treating computational security as absolute; exam answers must say "computationally infeasible under assumed hardness".
  • Mixing up public/private keys in diagrams (label clearly which is secret).
  • Using textbook RSA without proper padding (OAEP for encryption, PSS for signatures).
  • Reusing nonces/IVs in CTR/GCM/ChaCha20-Poly1305 (catastrophic loss of confidentiality/integrity).
  • Claiming MACs provide non-repudiation (they do not; only digital signatures do).

For your exam

Question types you will see: definitions (e.g., define perfect secrecy), short notes (compare symmetric vs asymmetric), derivations (show OTP secrecy or derive RSA d), numericals (RSA small primes, DH trace, keyspace/time), program sketches (hybrid encryption pseudocode), and diagrams (draw encryption/decryption flows). A full-marks answer includes: precise definitions (bold in your notes), assumptions, step-by-step derivations with modular arithmetic shown, final numeric results with units/quantities, and clear diagrams labelled as described.

Quick check

  1. What is the key relation that must hold for RSA exponents e and d?
  2. State Shannon's condition for perfect secrecy in one sentence.
  3. In Diffie–Hellman, if Alice computes A = g^a mod p and Bob computes B = g^b mod p, write the formula both use to obtain the same shared secret. Answers: e·d ≡ 1 (mod φ(n)); For all m,c: P(M=m | C=c) = P(M=m) (ciphertext gives no information about plaintext); s = B^a mod p = A^b mod p.

Notes for reviewer (accuracy/uncertainty): I have used textbook RSA example values (p=61,q=53,e=17,d=2753) and the known ciphertext 2790 for m=65. I approximated 2^128 and brute-force time with 10^9 tries/s. Check the numeric exponentiation for the RSA encryption and the constant approximations for keyspace/time. I avoided deep elliptic-curve math (next topic). Where I am unsure I list items under "verify" below.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?