C# Public/Private Key Encryption using Visual Studio 2019 | RSA Cryptography

C# Public/Private Key Encryption using Visual Studio 2019 |  RSA Cryptography

#PublicPrivate #Key #Encryption #Visual #Studio #RSA #Cryptography

Hello everyone welcome back once again in this video we’ll look at public private key encryptions with c-sharp using the visual studio 2019 but before we get started please make sure you subscribe to the channel if you haven’t so let’s get to it then cshapn.net provides implementations of many standards cryptographic algorithms

So the rsa being a public key crypto system has two keys the public key and the private key the encryption is done using one and that decryption is done using the other but normally the encryption is done using the public key and the decryption is done using the private key so enough of

All the talking let’s see how we can achieve that in code so as you can see i’ve got visual studio 2019 opened so let’s get to we can get started by creating a console app so we create a new project so we select the c sharp console

Application so this is a project for creating a command line application that can run on the net core on windows linux and mac os so we select that and click nest here you can configure your project details we’re not going to look too much into that here you can select it

Dot net i mean the net version or we can just click on the create so as you can see here we get in there so now we’ve actually got new console app created so we’re not gonna waste too much time so we’re just gonna go ahead so right after the

Namespace we’re going to create a new class yeah so we’re going to call it we’re going to it’s going to be a public class we’re going to call it rsa encryption so here we actually initial start with a new variable private static so we’re going to use the rsa crypto service provider

So we’re going to call it csv for chris crypto service provider we actually resolve the namespace by using the system.security.cryptography yes so we we instantiate it as well so it’s the new instance of it so inside here we’re going to put how many bits so we’re going to put a 20 48 bits

Yeah so the next base we’re actually going to start this variable is rsa parameters and this will be underscore private key so the next bit will be the same rsa parameters so this will also be a public key yeah so we grab the constructor so here we actually initialize both the

Private and the public key so the first the private key this will be equal to the csb the cryptoservice provider that expose or we export the parameters so for the private key we set truth and then for the public key we set up the csp dot export parameters

So here we set that to false as well yeah so as you can see it’s not it’s not really difficult so the next is the creative method is a public that returns string we call this method get public key so this method we initialize a variable called

Xw this will be a string writer a new instance of a string writer so the stringwriter will be on the system.io so we put after the syst we’ve put another variable we call it xx xx and this will be a new xml serializer so inside sms so here we can resolve it

This will also be under system dot xml serialization so in here what we do is we’re going to put a type of um rsa parameter and then what we do next we put xx dot serialize we’re going to serialize the public key so in here this is what we’re going to put

We’re going to put a string writer xw and then our public key yeah so this that’s how we’re going to actually print out a public so sorry return xw dot to string so that’s what we do to get our public key so the next method that we’re going to write is

Is a public string encrypt so we’re going to write our encrypt method so this will take in our string the plain text to be encrypted so this is what we’re actually going to put inside there so we grab our csp which is the cryptoservice provider we set the new instance of it

It’s a new cryptoservice provider and then our csb dot imports parameters so basically we use the public key to encrypt the data and then a variable record is our data to be encrypted this will be encoding so we grab an accordion with system.test accounting dot unicode dot get bytes

So we pass in our plane test and then um we start our variable this will be our encrypted test our cipher test so this will be csb dot encrypt so we pass in the data and then we put in force and then we return our encrypted test so we convert it to

To base64 string and then we pass in our cipher test so that’s all we do for our encryption part so the next part is of course we go to our decrypt we decrypt the encrypted message so we return the string as well so we call this decrypt

Of course and it would take in a string which would be our cipher test so here inside the decrypt we actually initialize variable which will be our data bytes we convert from base64 string Ciphertest and then csb dot import parameters so we grab our private key as well so what we do next is um we get our plane or decrypter test plane test this will be um csb dot decrypt so we pass in our data bytes and we put in force here so we return

Our decrypted test this will be encoded encoding dot unicode dot get string so we passed our plane test so this will actually be returned so let’s come inside the main method and see and print out some and do some operations so right after the console.rightline we’re just going to put console.readline

Yeah so here this is what we’re going to do so the first is we caught we actually initialized this class it’s iris encryption so it’s iris encryption we just call it rsa we initialize it then we initialize i mean we declare variable outside for test or just we just call it cipher

And then this will be of course string dot empty so the nest is a console.writeline so we’re just gonna say enter your test to encrypt so variable test will be console.readline so reader test that is written so what we do next is um we check if if um the test i mean string

Dot is not or empty the test so if it’s not empty so this is what we’re going to do we put our cipher here so we equal our sci-fi to rsa dot encrypt so we pass in our test so we could say i encrypted test this

Um our sci-fi we pass in the cipher so right after that there we could say press any key to decrypt the test so once that bit is done we’re just going to put a console.readline here so once that bit is done this is what we’re actually going to do um a variable

A plain test will be rsa dot decrypt so we pass in our cipher just to be decrypt okay so that’s all that we’re gonna need for now so we’re just gonna run it to see what actually comes out of it now yeah so this is first of all we’re just

Gonna show our pop i mean public key first um so before we do anything we’re just gonna put here um console.live uh public key so rsa dot get public key so that’s all we’re going to do that we just continue so here what we do is just going to put

A new line here so i’m going to run it again so as you can see here now we’ve actually got our public key being shown here so you can write a test here so this is the test to be encrypted by our encrypt method so basically this method will be

Encrypted so once we actually click uh enter so this is the encrypted test so as you can see here now we’ve actually got press any key to decrypt i should have actually put a new line here so once we press any key here we should get the test um decrypted back yeah

So as you can see the decryption decryption did not come through because we’re just gonna write it we’re just gonna print it out here so we’re just gonna say decrypted message yeah so here we can put the plane test and that’s it okay so he’s just running so i’m just

Going to stop it from running there we’re just going to save it rerun it again so so this is the message to be decrypted press enter so it’s encrypted so we press enter again and we’ve got that decrypted message here so as you can see it’s not very difficult

I’ll leave it here for you to explore further right so once again if you haven’t subscribed to the channel please make sure you do because i’ve got lots of videos coming up and i hope you have a lovely morning afternoon evening night wherever you are stay blessed and peace bye

Like it? Share with your friends!


What's Your Reaction?

hate hate
confused confused
fail fail
fun fun
geeky geeky
love love
lol lol
omg omg
win win


Choose A Format
Voting to make decisions or determine opinions
Formatted Text with Embeds and Visuals
Youtube and Vimeo Embeds
Soundcloud or Mixcloud Embeds
Photo or GIF
GIF format