Cryptography Fundamentals


Cryptography Fundamentals

Maria
Hey Simon, have you ever looked into how cryptography works in Node.js? I came across this talk that breaks it all down really well.

Simon
I've dabbled a bit, but honestly the crypto module documentation is pretty overwhelming. It's just a flat list of APIs with little context.

Maria
Exactly! The talk mentioned that. The speaker, Jonathan, felt the same way. He recommended a book called 'The Code Book' to understand the fundamentals before diving into code.

Simon
I've heard of that! It covers the history from Caesar ciphers to modern encryption. That would give a good foundation. So what are the core concepts we need to know?

Maria
Let's start with symmetric encryption. It's like a lock where the same key locks and unlocks the message. Alice and Bob share a secret key, and Eve is eavesdropping.

Simon
Right, so Alice encrypts plaintext with the key to get ciphertext, and Bob decrypts with the same key. But which algorithm do you actually use in Node?

Maria
There's a function `getCiphers()` that returns 171 available ciphers. A common one is AES-256-CBC. AES is the Advanced Encryption Standard, 256 bits for the key size, and CBC is a mode that splits data into blocks.

Simon
And I remember that CBC requires an initialization vector, or IV. Why is that important?

Maria
The IV is a random value added to the first block. Even if Alice sends the same message every day, the ciphertext will look different each time. That prevents Eve from finding patterns.

Simon
So in code, you generate the IV with `crypto.randomBytes(16)`, then pass it to `createCipheriv` along with the key and algorithm. Then `cipher.update` and `cipher.final` produce the ciphertext.

Maria
Exactly. Decryption is symmetric: `createDecipheriv`, `decipher.update`, `decipher.final`. Now, what about the key itself? You can't just use a password directly, right?

Simon
Right, because human passwords are too predictable. That's where key derivation functions come in. They take a password and a salt and produce a strong key.

Maria
And KDFs like `scrypt` are intentionally slow and memory-heavy to make brute‑force attacks impractical. The salt is random, so even with a precomputed list, Eve has to start over for each salt.

Simon
So you call `crypto.scrypt(password, salt, keylen, callback)` to derive a key. Node also supports `pbkdf2` and `hkdf`. But generating good random values is crucial throughout.

Maria
Absolutely. Never use `Math.random()` for crypto. Use `crypto.randomBytes()`, `randomFill()`, or `randomInt()` instead. They are cryptographically secure.

Simon
Now, symmetric encryption has a problem: Alice and Bob need to share that secret key in advance. On the internet, that's the key distribution problem. How do we solve it?

Maria
Enter Diffie‑Hellman key exchange. Alice and Bob can agree on a shared key over an insecure channel without Eve learning it. They each have a private key, exchange public values, and combine them to get the same secret.

Simon
In Node, you'd call `crypto.createDiffieHellman(prime, generator)`. It takes about 20 seconds to generate the prime, but then you can exchange public keys and compute the shared secret.

Maria
And there's also elliptic curve Diffie‑Hellman (ECDH), which is more efficient. But Diffie‑Hellman still requires a round trip for each new communication. RSA solves that differently.

Simon
RSA is asymmetric: you have a public key and a private key. Bob publishes his public key, and anyone, like Alice, can encrypt a message with it. Only Bob can decrypt with his private key.

Maria
So no key exchange needed. Bob generates a key pair once with `crypto.generateKeyPair('rsa', ...)` and exports the keys. Alice uses `crypto.publicEncrypt(publicKey, plaintext)` and Bob uses `crypto.privateDecrypt(privateKey, ciphertext)`.

Simon
I've heard that quantum computers might break RSA eventually. But for now, it's still widely used. Now, even with all that, how do we know a message really came from Alice? That's about signing.

Maria
Exactly. Alice signs a message with her private key using `crypto.sign(algorithm, data, privateKey)`. The signature is sent along with the message. Bob verifies with `crypto.verify(algorithm, data, publicKey, signature)`, which returns true if it's valid.

Simon
So only Alice could have created that signature. But that assumes Bob knows which public key belongs to Alice. That's where certificates come in.

Maria
Certificates bind a public key to an identity. They contain the subject (who it's for), the issuer (who signed it), validity dates, and the signature. Alice gets a certificate signed by a trusted authority, like Carol.

Simon
So if Bob trusts Carol, he can verify Alice's certificate using Carol's public key. That creates a chain of trust up to a root certificate baked into your operating system.

Maria
In Node, you can parse certificates with the `X509Certificate` class and verify them using the issuer's certificate. And a real‑world example: COVID‑19 vaccination QR codes used signed JSON data. The public key was on GitHub, so anyone could verify offline.

Simon
That's clever. So we've covered symmetric encryption, key derivation, random numbers, key exchange, asymmetric encryption, signing, and certificates. That's a solid foundation.

Maria
And everything is available in Node's `crypto` module, built on OpenSSL. No extra installs needed. For browser JavaScript, there's the Web Crypto API with similar concepts.

Simon
This talk sounds like it would really help someone get started with crypto in Node. I'll check out the code examples he mentioned. Thanks for the overview!

Maria
You're welcome! It's a fascinating field, and understanding the basics makes using the APIs much less intimidating.

Ask anything
We've read this whole study set, so questions about it get the best answers.