1. Home
  2. Explore
  3. Computer Science
  4. Cryptography Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Cryptography Fundamentals

Speaker 1Speaker 2

Cryptography Fundamentals

4:55
0:00-4:55
  1. 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.

  2. 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.

  3. 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.

  4. 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?

  5. 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.

  6. 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?

  7. 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.

  8. Simon

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

  9. 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.

  10. 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.

  11. 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?

  12. 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.

  13. 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.

  14. 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.

  15. Maria

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

  16. 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?

  17. 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.

  18. 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.

  19. 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.

  20. 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.

  21. 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)`.

  22. 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.

  23. 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.

  24. 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.

  25. 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.

  26. 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.

  27. 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.

  28. 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.

  29. 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.

  30. 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!

  31. 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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent