Cryptography is the science of secret communication, and Node.js provides a built-in crypto module (based on OpenSSL) for implementing cryptographic operations. The module supports symmetric encryption (AES with 256-bit keys and CBC mode using random initialization vectors), key derivation functions (scrypt, HKDF, PBKDF2) for deriving strong keys from passwords, secure random value generation (randomBytes, randomFill, randomInt, randomUUID), asymmetric encryption (RSA) for solving key distribution problems, digital signatures for message authentication, and certificate chains for establishing trust through trusted authorities.
Cryptography Fundamentals with Node.js Crypto Module
Added:everyone thank you for coming uh so today I'd like to talk to you about this okay now it's much clearer right um so wait the title of the talk is encrypted so I just need to apply the decryption key okay here we go so uh mastering cryptography fundamentals with nod crypto modu that's a bit of a mouthful so let's break it down so first what is cryptography so I'm sure you have heard recently about crypto uh but not in the context that you're probably thinking of so crypto in Greek means hidden and that this is why cryptography is the study of or the science of uh techniques of secret communication or secret writing is there problem yeah sorry thanks that's better so as you can imagine having a way to uh communicate in secret um and encrypt our correspondence plays a major role in today's on online World um and because of that it's a good thing that inside of node.js we have a built-in library for handling cryptographic operations and um and uh cryptographic algorithms it's one of the built-in libraries so you can just get start using it right away no need to install anything just like uh the fs model for example and it's based on top of op SSL so it's a JavaScript wrapper on top of op SSL and that's um an open Source widely used library that handles these cryptographic um algorithms now because it's one of the built-in modules it has documentation up on the nodejs website which means you can go and learn about what are the different apis but for me personally when I was became interested in this topic I became a bit frustrated when I tried to use this as a resource because what I realized is that um the problem was really more with me because uh this is a very like flat uh alphabetical list of apis and it gives very little context about each algorithm it kind of already assumes that you know what you're trying to do what is the algorithm that best is best for that use case and then it just gives you the specific syntax in node GS um so I had to go elsewhere and learn more about the the fundamental concepts before this made any sense to me luckily we have other great uh resources some of them free online to learn about the basics uh of cryptography uh this is a book that I would really recommend if you're interested um so it's called the code book and it talks about the history of cryptography not just in like modern Computing but going all the way back to Julia Caesar and it's written in a very simple way to understand the concepts and also it's very fascinating because it talks about the roles that these advancements in cryptography how they played a role in um the political and Military struggles of the time so you can understand how much of an impact this uh type of uh study or uh or science has and in fact I find it kind of cool that today on the 50 pound bank note in the UK you have uh someone who is a cryptographer a cryptography researcher this is Alan Turing who played a major role in uh breaking the Enigma machine encryption and so obviously he did amazing things but I kind of like to think that it's pretty cool because in a way he is a programmer just like one of us so I'm like imagining somebody going to build stuff and then 40 years later they put his face on the money that's that's pretty cool uh so what I want to do in this talk today is uh to kind of give you a a shortcut through the path that I took so to learn and cover the basic concepts that we have in cryptography uh to understand them at a theoretical level but then to make sure that we're covered in a practical way to actually get started we're going to see how we actually do things in code with the crypto module so let's dive in my name is Jonathan M I work at Wix this is where you can find me online so the first thing that comes to mind when we talk about cryptography is this idea of encryption encryption is what lets two parties uh communicate over an insecure Channel something where they they they take under consideration that somebody might read their correspondence and because the message is encrypted they don't have to worry about that party to to kind of understand the original message that they're hiding with the encryption process so uh when we typically talk about cryptography we use this couple as an example Alice and Bob now unfortunately we also have Eve who is uh here to represent like a potential hacker or malicious uh actor and she and when Alice and Bob communicate they're going to have to assume that Eve is there uh EAS dropping uh now Alice and Bob came to build stuff today luckily Eve is nowhere to be seen [ __ ] okay she's here so we have to watch out and we have to apply encryption very carefully all right so for this first type of encryption that we're going to show um Alice and Bob need to have something which we called the the the shared key and this is a value that they both know they both need to use the same value which means they also have to agree on which value to use as the shared key and it's important that um Eve will not have access to this value because if she does she'll be able to perform the decryption so and we're going to talk a bit later about how we decide exactly on what the key is so let's start with a mental model of how the encryption work and uh get familiar with the basic terms so uh to apply the encryption algorithm Alice takes the plain text uh and the key uh the plain text is the original message and she passes it through the deterministic encryption algorithm and what you will get is the cipher text so for example if this is the the message then the cipher text will look like this something which uh looks meaningless and this is why even if she sends it over an insecure Channel over some Network that she can't trust and Eve is there to read the message nothing bad happens and then we have Bob who is able to apply the decryption in order to read the original message so when we do this in real life there are a couple of more things that we need to uh be aware of and the first thing is Which comp uh which um encryption algorithm do we choose because we have many and overall they all work by performing a set of substitutions and transformation on the original PL text to derive the Cy text so this is the first bit of code from the crypto model that I want to show today and this is the get ciphers function and as the name suggest this returns a list of ciphers or uh cryptographic algorithms that are available for you to use so when I run this on my machine this returns 171 different algorithms so you have many options and the one that we're going to take a look at in this example is part of a family of protocols or algorithms uh which is called Advanced encryption standard and as the name suggests it's a standard so it's widely used and it's implemented in different languages so that means that you can do the encryption in ogs and somebody could do the decryption in python or net or any other language and it's considered Advanced enough so uh it's still relevant today we have other algorithms that because of the increase in computer power are considered outdated today so this is still relevant now specifically we're going to take a look at AES 256 this stands for the size of the key so um we want to choose a key that's uh that that's that's um uh from a range that's big enough so that if somebody like Eve is trying to guess the key they will have to uh try to if they if they're trying to Guess The Key by brute forcing and trying all different combin ation of the key they will take them a really long time to do so 256 in this context means that the size of the key that we're going to use is 256 bits and CBC stands for Cipher block chaining and this is a mode of encryption where we split the original uh plain text message into these uh blocks and then we encrypt each one separately now in order to compute the very first block we need to supply something extra which is called an initialization vector so let's talk about what value we use as the initialization vector and why we have it in the first play in the first place how is it making the encryption more secure um so this is the revised encryption process it's exactly as before but now we have to pass in the IV as well so uh the value that we use as the IV is a random value that means that even if every morning when Alice wakes up she sends Bob the same message hello which means the message will be the same and the key will be the the same she will still use a different IV value every time and because the encryption algorithm is deterministic this means that the uh resulting Cipher text will be different every time and this is why it's making our encryption more secure because um it's preventing an attacker like Eve to try to guess our uh plain text and find patterns and relationship between the original uh plain text and the resulting Cipher text and that way to try to guess the key so uh this is from Alice's point of view and then she sends Bob the IV the cipher text and the algorithm or they can just choose to use the same algorithm every day and then it won't need won't need to be part of the message but at any rate now that Bob has this he can apply the decryption using the same IV value that Alice used so let's see how this is done in code um so this is the first Alice's encryption logic um so we have the algorithm like we said this is the name of the algorithm the plain text is not used as a string it's converted into a buffer we never use strings directly when we're doing cryptography then we have the random value generated for the IV using the random bytes function then Alice loads the key into memory we'll talk later about uh how exactly we decide on what the key is and then she creates a cipher object using Create Cipher IV passing in the algorithm the key and the IV and then to compose the actual Cipher text uh she calls cipher. update passing in the plane text you can call this many times if your if your plain text is very big and you're composing it over time and when she's done she calls cipher. final and this gives the resulting Cipher text now to perform the decryption notice how much similar looking it's like the mirror uh image of the encryption logic that Bob uses so he gets the IV the cipher to extend the algorithm he loads the key into memory and then he calls create decipher IV decipher being the opposite of Cipher and then again very similar looking API calling the update method on the decipher object and this gives the original plain text back now uh we kept it pretty vague so far what value we use as the key now Alice and Bob might be tempted to use a human memorable password that's easy to remember as the key itself but this will be a mistake why is this this is because we mentioned before that we purposefully use a a wide range for the size of our key so that if the attacker if she's trying to guess the key she she will have to try a very large set of options if Eve can assume that Alice and Bob are using a human memorable password as the key then Eve will be able to try a much narrower set and it won't even help if uh we're trying to be clever as users by replacing like o's with zeros uh this still leaves a very very narrow set of options to use so we don't want to use um the password as the key itself but what can we do if we still want to base this our security about something that that we remember so one of the options to use is something called a key derivation function or kdf for short and as the name suggests this is uh something that lets us derive a strong Long Key from something uh else for example a password that remember okay so uh this works like very similar to a hash function if you're familiar with them to check like the Integrity of uh the check sum of the of some file but interestingly uh unlike a normal hash function or unlike most functions a kdf is actually designed to be slow on purpose uh it's meant to be CPU heavy and also memory heavy why is this uh we're making this this function slow on purpose so that if Eve is trying to take again a list of common passwords let's say she finds it online and she's trying to Loop through them and compute the resulting uh um key using some well-known kdf it will be very slow for her to do and make it not worth her while so problem solve right but if you think about it if Eve is smart what she can do is she can go online and not just download the list of the 10,000 most used passwords but also find uh their value when pass through some well-known kdf and use a pre-computed list that somebody else has already taken the time to run this costly function many times so if this was the actual behavior of the kdf then the kdf is not making our correspondence any more secure and it's just making the process more complex this is why in real kdf's we have another crucial parameter which is called the salt and again what we pass to the Salt is a random value so this means now that Eve uh even if she gets a hold of our salt she will and she's trying to guess our password using a list of common passwords she will still need to run this process of taking the password that she's trying to guess our unique salt combining them running this very slow function just to resolve just to get these uh keys that she wants to try and hopefully this will make her just give up on the entire process so those are kdf's let's see how this looks in code um so very similar to the process we showed before I'm highlighting only the parts that change one of the kdf's that's built into the crypto module is a kdf which is very popular which is called SC script or secure Crypt and uh basically you can see here the two parameters that we discussed the password and the salt uh where we get we generate with random bytes and uh you pass them to script and you get a key that you can later use uh another notable mentions here we have two other kdf's baked into the crypto model hkdf and pbk df2 that's a bit of a long name all right so as we saw so far being able to Generate random values is super critical to uh getting cryptography right so let's see how we can Generate random values so the first rule of doing that is not to use math. random which you're probably familiar with if you're working with JavaScript for a long time because as the mdn doc state it is not considered cryptographically secure which means that an attacker can either guess what this will return on your machine or they can um make you think that you're generating something random and in fact this is something that was uh planted there by the attacker so it's not what you want to use so what can you use instead so inside the crypto module we have several ways of generating random values all of these are asynchronous but they use a callback style which is kind of not popular these days so if you want to use them as promises you need to call u. promisify on them so the first one is the one we showed before Rand bytes you give it the size that you want and it returns a new buffer of that size with random values inside the other one is random fill this takes an existing buffer and either replaces all of it or a subset of it with random values you have random int uh given a minimum and maximum range it returns an integer in that range with with a random value and you have random U ID um so the encryption process we showed so far it security really hinges on the fact that Alice and Bob have this shared key that they both know and only they know and no one else so this means that they have to exchange it in a way that's offline so this was nice uh maybe uh tens or hundreds of years ago when two parties would meet decide on the key and then go their separate ways and exchange physical like letters but uh I hope that you realize by now that Ellis and Bob represent computers so this is not really relevant for computer use right and in the in the internet and networks we have different computers on the other side of the world they don't have a easy way to meet offline in a coffee shop and decide on a cute shared key that they will use so what can we do uh so this is something that Alice thought long and hard about and honestly this is something that I thought long and hard about as well and this was something that a lot of smart people thought about for a long time as and this was became known as the key distribution problem now luckily uh just in time for when the internet was taking off uh a group of scientists in the 1970s in the US came up with an elegant solution to the key distribution problem so let's see how this algorithm works the algorithm is known after uh the creators this is the Dy Helman key exchange so let's see how this works in order for Alice and Bob to generate a shared key uh even though they're communicating over an insecure Channel and end up with a shared key that only they will know this is the steps they will take so Alice will get a prime a generator and her private key and she will use them to derive her public key then she will send these over to Bob Bob will do the same with the same Prime and Generator but with his his private key which will generate his public key then he will take Alice's public key and use them to derive the shared key then uh we need to go back and forth right this is a key exchange he will send the public his public key back to Alice and then she can derive the shared key as well I know this looks like very like random values and stuff it's not really important what exactly each box represents but I just want you to the main thing I want you to take away is what they both end up with and that is that even though they communicated completely in the open and they're taking into consideration the fact that uh Eve was reading all of the messages that they sent back and forth Eve has no way to come up with the number in purple and uh Alice and Bob both do have this identical value so this is why this was a very groundbreaking achievement let's see how this is done in code uh to do the key exchange this is Alice's code um first she calls create Dy Helman this on my machine takes 20 seconds so it's a very slow uh function to execute and um so and then she gets the prime the generator and uh her public key the private key is uh inside uh um is hidden inside and then she can send these over to Bob Bob gets these things uh he calls create Helman because he's using an existing Prime and Generator this is much faster this time he gets his public key and he can also already compute the shared key and then on Alice's side she can compute the same shared key and then they can start encrypting using this key in the same process we showed earlier um another notable mention here is something called elliptical curve Dy Helman uh also a product of this same group of researchers and something that's widely used today so you can read about it more in the uh docs so like I said this was a groundbreaking achievement but if you think about it we still have some disadvantages right because we still have to perform this like dance of the key exchange with each party that we want to commun Comm unicate with and that's kind of a shame so now we're going to take a look at uh also in the 1970s also a group of uh uh researchers from the US who came up with a different solution to the key distribution problem which even solves these disadvantages of the Diffy Helman key exchange and the algorithm that they come up with is known after their initials RSA so uh now really there's no need to do any key exchange uh and we're going to understand why so the first thing that that Bob has to do is get two numbers one can be derived from the other um a private and public key and then he makes this key known to the world this is another advantage of RSA versus the Diffy Helman key exchange where with the Diffy element key exchange you have to do this uh key exchange with every party that you want to communicate with and here he makes this publicly known to everyone and anyone can use it to uh to start encrypting and sending Bob messages so if Alice wants to send Bob an encrypted message she takes Bob's public key the plain text message and she from these she can derive the cipher text and then the cipher text is sent to Bob and using his private key he can do the reverse decryption so because we have now a different key one for encryption and one for decryption this is how we completely avoid uh the key distribution problem and of course it sounds very simple when I say it but uh the trick was to find you know the math that that makes this work which you can read more about in the book if you're interested um so now that we have this process um this really makes our lives much easier so let's see how this is done with code in code so this is uh first how to generate the keys Bob needs to do this like I said only once not just for communication with Alice so he calls uh generate key pair which generates for for an RSA key paare and this generates the two keys and then he can uh save them to file the public key in order to share with others the private key to store in some secure Drive um and this is how you export these keys to a file now in order to perform the encryption using this what we call asymmetric encryption because we have uh different keys for encryption and decryption this is how Alice does it so she calls uh she reads Bob's public key into memory she takes the private the sorry the the plain text message as a buffer and she calls public encrypt and this will give her the cipher text she needs to pass the public key uh the plain text and a configuration value which is called the padding and then to perform the decryption um so Bob gets The Cypher text uses his private key and calls private decrypt in order to perform the decryption passing in the same parameters uh the key the cyer text and uh the same padding value and this will give back the original plain text um something that you need to be aware of uh pretty like recent stuff is that uh RSA is beginning to show its age and um we have researchers who using a quantum computer say that they were able to break RSA encryption I don't think this is something that you need to worry about uh this is for the people working on very cutting age stuff uh even if somebody has some entity has this capability they're not going to waste it quote unquote on uh the simple things that are in the commercial space uh but it is something to be aware of and I think I did read that um Apple in um iMessage they already using a type of encryption that they were able to prove that is uh can be protected against um being cracked by quantum computers even though they're not that widely available um so now it's time to think have we solved all of the problems in the cryptography space or are there any problems still left well now every every person every entity on this network has their private and public key they can send messages back across but what we don't have yet is a way to know that the message that we got from a certain entity really came from that entity and that plays a major role right we want when we get a message we want to make sure that we trust that it really came from who we think it came from so this is where we get into the world of signing and verifying so for the sake of this example let's say that Alice is one of the organizers of uh build stuff and she wants to send Bob a message saying that he can go to the conference so obviously Bob needs to be able to prove to himself and anyone else that the message really came from Alice so let's see how this is done uh for the sake of this example the message itself is not encrypted but it can be um so what we do is what Alice does is she takes her private key and the message and she creates a signature which looks like a again like a value of um if you pass it through like a a hash function it looks like just a random value um but the signature is the the the key thing that it's that's going to make it so that Bob can verify that the message really came from Alice so her public key is known to everyone and she sends Bob the message and the signature it's fine if other people read this message as well and then Bob to be able to prove to himself or to anyone else he can take Alice's publicly the message and the signature and run it through this cryptographic verification process and see if this returns that the signature is valid or invalid now the the main thing here is that only someone with access to Alice's private key which we hope is Alice can create such a signature that when combined with Alice's public key will come out as something that's verifiable so let's see how this is done in code uh in order to create the signature uh Alice reads her private key into memory she takes the original message and she calls crypto do sign and she needs to pass in the hash function to use as part of the signing process the message and the private key and then Bob can perform the verification he reads uh Alice's public key into memory and he calls crypto verify and this will return a bullion of true or not he needs to pass in the same parameters uh which hash function to use the message the uh the alysis public key and the signature itself and this returns true if all of the math and the cryptography all line up another notable mention here we have something called create hmac uh which stands for a hash based message authentication code and this goes to the world of uh signing in the world of uh symmetric cryptography when we don't have uh a different uh uh Keys um and it's something that's quite popular today you can also read more about it all right so uh now I want to take a slide DET tour and talk about when do we ever get to use this stuff because this is like the basis of htps and SSL when do we get to roll up our sleeves and build something interesting with cryptography so this is one example I'm not sure how it was here in Lithuania but three years ago uh where I'm from if I wanted to go to a coffee shop I had to do this fake dance with the host where I would show him uh the QR code that proves that I was vaccinated now it was a complete joke because they never bothered to scan the QR code so I could have just printed a QR code that leads to a rickoll video but at least in theory uh somebody thought about an elegant solution there because what these QR codes that we got what they contained is a standard by the EU uh which contains not an encrypted but a plain text um Json value which has your name what vaccine you got and when you got it and a signature to make sure that it's legitimate now the signature was created with the private key of the Ministry of Health and the public key was actually available for everyone on GitHub so either you could use their application and scan the code or you could create an app yourself that works completely completely on offline no internet necessary that can read the message which is the Json details of when you are vaccinated get the signature and validate it using the public key so I think kind of an interesting usage of this type of uh technology so again I'm asking the question are we done did we solve all of the problems in cryptography can we trust everyone on the internet now well I want you to think about the process I showed for signing and verifying and if you think about it it didn't really prove what we said it would because I said this will allow us to prove that the message really came from Alice but all it did is make us uh it helped us prove that the message came from some some entity somebody posting online that says I'm Alice and this is my public and this is how you can verify that message came from me but really now the weakness in our system becomes how do we know that a specific publicy really does belong to a certain person or entity so this is where we get into the world of certificates or publicy certificates because they help establish ownership and prove ownership of uh certificates in general so before we dive in I just want us to cover uh what we talk about when we talk about certificates in general okay so this is a a certificate not a a cryptographic one right so this was a certificate certificate that I got uh and let's go through the part so it has um who was it to okay this is my name so this is who it was issued to we have some claim that the certificate is giving us and this that the person who gets the certificate me is the absolute best at remembering git commands without ever needing to consult uh documentation and this was given to me by someone uh this is uh by the creator of git and you can tell it's legitimate because we have this signature right so these are the four parts that we have in a certificate who is it for what does it say about that person uh who is the person giving that uh making that claim the the person who issued the certificate and we have the signature now did I really get a certificate from the creator of git for being the absolute git Master uh I'll let you decide but whether or not you trust this signature luckily in cryptographic uh digital certificates we don't have to trust anyone or apply this judgment we can actually verify the signature so this is why this process is a lot more effective and we trusted so um what I like about this though is that now if Bob wants to start communicating with Alice and he wants to know that the message really came from Alice really there's no magic solution I'm not going there's no like uh new algorithm that uh solves this problem it goes back to the very simple concept of in order to trust someone new I need to trust some other party that we both know so this is Carol I hope you can tell by the close that this is Bob's sister and she's also a friend of Alice and she's going to uh help establish this initial trust between Alice and Bob so what Alice now wants to do is create a certificate uh that uh gives her that proves that she is the owner of some public key that she posts on the internet for everyone to start communicating with her so in order to create the certificate what she will do is she will take the public key which the main thing that she wants to establish ownership of add some U meta data about herself what's her name what country she's from Etc and she will create something which is called a certificate signing request um and and this is this is the part of the certificate which is to be signed TBS to be signed uh but it needs to be signed by someone and that person that's it's going to be signed one is signed by is the other entity that that wants to help out Alice and uh kind of use her uh reputation in order to help out Alice so that people that trust Carol will trust Alice as well so car if she's willing to help she takes the certificate signing request she takes the TBS the 2B sign data and she signs it with her private key and then Alice creates a certificate using the signature that she got from Carol and then she makes this available to the world and then anybody who already trusts Carol can go and take Carol's publicly analysis certificate and and make sure using the same verification process that we showed before that the signature really does matter match along with the metadata and everything else to carols publicly and this let lets um Bob in this case know that if he trust Carol then he can trust that this is really Alice's certificate and therefore he can trust Alis as well so let's see how this is done in code so to read a certificate into memory we have the x509 certificate uh class this is the name of the specification for how to structure certificates so we read the certificate into file and part and parse it this way then then we can read metadata on the certificate what is the subject of the certificate who was it given to CN stands for a common name so common name Alice we have the issuer in this case Carol we can see valid from to valid to what is the uh date range of that this certificate should be considered trustworthy and then to perform the verification um what Bob can do is he can read Carol certificate in the same way Carol certificate uh contains Carol's public key and then Carol's public key can be used to verify Alice's certificate and if this this returns true if everything lines up so again I I'm asking you are we done are there no more problems left well if you're very skeptical and I hope you are you should be in the world of cryptography how can Bob trust his sister Carol well how do you begin to trust your sister you have your mom and if you trust your mom she tells you to trust your sister so what I'm trying to illustrate here and you can see this if you go in your browser and uh take a look at a certificate which you can do is you have something which we call a certificate chain or a certificate hierarchy and um what and when you go to a website you download some certificate you want to verify it you need some other certificate that you don't have so you go get that one and at the very top you reach your mom certificate or your root certificate which is a certificate that you just have to trust because it's baked into your operating system and not because you download some other certificate all right so uh We've covered these Concepts um so uh we have you know encryption ke kdf's Randomness the asymmetric encryption or the key distrib or solutions to the key distribution problem and the world of signing verifying and certificates another notable mention if you're doing JavaScript but not in node.js and rather in the browser you have something which is called the web crypto API which the apis look different from what I've showed but it covers the same uh you know the same algorithms so uh you can learn more about that as well uh here you can find all of the code from uh The Talk today and because it's cryptography and we don't trust anyone it's covered by unitest so you can run them to make sure that it's actually working so don't trust anyone uh this is where you can find me online I have a big blog post which talks about this cryptography in OJs which you can read and also some other projects this was also during covid I was very bored alone in my room so so I took apart a arcade machine and I made it show grafana graphs instead you can read more about that and um thank you for listening if you have any other questions um you can ask Alice and Bob later thank you
Up Next

RWA Tokenization: Complete Guide to Institutional Asset Management
@defactor_
801.3K views•2025-03-25

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Operational Security Essentials: A Guide for Hacktivists (OPSEC)
@hitbsecconf
157.4K views•2012-11-26

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto

![[27C3] (en) Node.js as a networking tool](https://i.ytimg.com/vi/EGpSA43yxYM/hqdefault.jpg)











![Hachage cryptographique - chapitre 2.1 [Crypto 301]](https://i.ytimg.com/vi/dvnGArYvVr8/hqdefault.jpg)

















![Robert Kawecki — Web Crypto: demystifying cryptographic operations in the browser [EN]](https://i.ytimg.com/vi_webp/bSg6vvi6TiQ/maxresdefault.webp)







