[Music]
hello everyone welcome back to mr.
random generator today I will be showing
you how you can implement RSA algorithm
using node.js okay so start off it we
first need to create app our project
folder so I just go ahead and create a
great supporter RSA example and we just
open it up on Visual Studio code ok
after we have opened it we need to
create our a signal with no J's project
so I just hit M p.m. in it and pass the
arguments Y which automatically okay so
we just initialize our project now what
we need to do is install a package
called node or SA and to install it we
just type NPM I or install node RSA
like that installed and also I will
provide some really useful links in the
description be sure to check them out
and do not RSA NPM page is one of them
so now we can now install that we can
just create a new file let's call it a
generator I will explain that in a
minute
generator touch ok so this is the file
where we create a keys and to do that we
just need to initialize and include a
packet in our project so we just type
RSA equals required and since I want my
keys to be stored on files I also need
to include bio system module which
enables us to make read and write
operations in our file system so we just
include file system yeah we also need to
just
yeah this is the correct way so yeah in
our RSA algorithm as you know we have a
public key and a private key
however the node RSA module generates
using a single method then it actually
generates both in a single method than
we export clips so to make things a
little bit easier I just create a
function
it's called generates pair which I'll
not take any input and always do not
motorist age because we just named it
the RSA so yeah and as you can see it
does have a method called generate key
pair so we call that now if you run that
we just made our keys but we need to
export them as well so
our public key
as you can see we have an export key
function so what that does is expose the
key according to the spec of
specifications that we give it to it so
in our case we just want our T to be we
just want to export public key from that
so we just buy public and you can again
they receive variations in the MPM page
of the know there's a module and we just
did do the same thing with the private
version so just privacy and make that
private so now we have both of our keys
we want to write them in a file and
write them on a file we need to again
use the filesystem module first we need
to open our files so we use open sync
method and in that method what we want
to do is just give the path our file and
a pass an argument which specifies the
operation we wanted to do with it so
what I mean by that let's say this is
our directory and this is the operation
of you want to do in our case it should
be right so we kill the argument W
stands for right and as for the fact
let's create a new folder and give it a
name case and in this folder we just
want to create a new file which is
public that p.m. which is the extension
for generally keys and we also wanted to
create a private dot PDF you can also
use key as an extra extension by way but
let's go go with BPM for the world so to
open those files let's give the
directory as you can see keys and let's
open the public ones public public
that'd be so we just did that and now we
want to write write the actual heated so
we just call a function write file sync
which writes its best
obviously synchronous manner and the
thing we want to write I can we also
need to specify it around here so we
just go out to keys and public p.m. and
after that we want to what we want to
give is the actual key itself so there
we go
and finally we need to mention the viola
including which is utf-8
just like so and after we did that we
can just copy it again and just make the
public ones private and it should be
working so after that we obviously need
to call the function itself so we just
it generates a pair and let's try it out
save it's the obviously needs to change
that as well
now we run the would generator that yes
it is running this range it should be
done there we go
now it's done and let's check our files
as you can see it's successfully written
deep public key and the private key
this is how we generate and write our
keys in a file but if we want to use
that we again we need to do something
more so let's create another file let's
call a purchase and we obviously need to
import this stuff
[Music]
and actually let me see if we can
company yeah we can copy those as well
but we don't want to export methods okay
let's go over one my gosh
so we import tools and we just again
public key and we just initialize them
actually empty we don't want to do
anything right now because we want to
import them from our files the only
reason I'm showing this is how you can
import it and basically using some
applications and I mentioned those
applications in a second so we just
created empty versions and to read those
let's say we want to read the public key
just use again our file system and we
now use read file sync just like the
right one we want to give the keys and
let's say yes it's public version so
public p.m. and now what we want to do
is give again the encoding which is
utf-8
duplicate it's and make the same version
with the private one
so private this should be private as
well okay now we have read from files
what you need to do is extract the the
key value from itself and put it in our
key objects which we mentioned right
here so to do that we just it's public
key that it does have an import key
function and what we want to pass areand
is the variable that we have read from
the file
and again what to do that T
fortunately private version as well yeah
right so now we can use our keys and
like to give an application idea what
you can do with that is basically create
license keys so it's obviously really
simple version of license key but we can
just get a cup so function creates
license so we got to go to Bisons and
that's for the input let's say we want
our users to be inputting their mail
address so you just give it and mail
let's say - we want to basically make
the reverse engineering a bit harder
task for them so what we want to do is
create salt which just passive a world
of saying like the random string let's
say just make something along those
lines I don't even know
you obviously you can use fancy password
generator as a thread etc to use that
and again we want to use another salt
it's all second for naming convention
just copy that
just change that change it a little bit
okay now we have created our source what
we need to do is we just want to create
the encrypted version of them so we have
closed encrypted to encrypt a message
what we want to do is use our private
key not practical and we want encrypted
using encrypted private method because
we want to encrypt the message and then
we want that message to be basically our
license key and we want our users to
enter in enter to that to their
application and in that application we
just want it to be decrypted by using
the public key so we can cross-check the
message and we can say it is valid or
not if we wanted the opposite way each
user must have private key that we need
to encrypt by using their each public
key which is not wise choice because
there are many public keys available for
many users because of their every user
must have any private key and those
pairs are again
assuming that they have vast amount of
them out there anyway let's go in Crips
pride and in this function what we want
to do is we want to give our salt first
plus nail and plus salt ii and the
encoding buffer as you can see which is
deal base64 so this is the encrypted
version and we just return that so let's
try that create license we just give
like
[Music]
hey that's example.com
save it again we obviously need to log
it so you can see what's and there we go
you have just created the license so to
use it what we need to do is create a
function called check validity so let's
do that
let's see and this function takes our
license some arguments so and in in this
case we want to first stop is obtain the
decrypted version and compare it with
the original message so we just create a
very variable called decrypted we should
use public key that's decrypt again
since we're going kind of the opposite
way we want to use the script public but
and in that method what we want to give
is the our license and again the utf-8
which
so and we just want to create a super
basic if statement like copy that and we
just inserted a at example.com and we
just copy the second one
based right there and if that is
basically equal to the decrypted one if
not so let's try it out so this is our
license so copy that and you just want
to run the let's check validity so we
just check validity just call it and you
are licenses in our arguments save it
[Music]
and it's not very huge check oh yeah
yeah yeah validity
because I'm using a different keyboard
not my super loud mechanical keyboard
with blue switches because - as you can
see now we run it it basically it turns
the true so we can see that it is valid
so I know these concepts are not that
very simple to grasp on but I'm sure you
will get its in a reasonable amount of
time so hope you can hope you liked this
tutorial if so please be sure to hit
like button and if you want to see more
content like this please consider
subscribing thank you take care see you
next time