Hello everyone. Here I am back with my second blog. In last blog we discussed what is Asymmetric encryption and what are it's type. We discussed RSA encryption technique with its mathematical computation in detail. So in today's blog, we will discussed how to use RSA encryption technique in real world.
I will use VS 2012 with .Net framework 4.5 to demonstrate the code.
I am going to make a windows form application for demonstrating purpose. Follow below steps to make windows form application:
1. Open VS 2012
2. Select a new windows application project i.e.(File->New project -> Windows application template) and give it a meaning full name.
Add a class file to the solution and name it as encryption
In Form.cs file declare the variables that are required. Please refer below code
Now to generate the public-private key pair use below code. Place this code in encryption.cs file
Let us look at above code what it's exact functionality:
Private Key:
In code behind file of encryption button paste below code:
Note: You can place all these code inside the form.cs file also. I have separated it to make code more understandable.
Encryption Functionality:
Place the below code in encryption.cs file
Let us take a look at above code:
GetMaxDataLength(): This function is used to check the length of the data after it is converted into bytes. The maximum bytes which RSA support is 245 bytes if padding is not applied or else 215 bytes if padding is applied. We have specified above the padding variable.
IsKeySizeValid(): This function is used to check whether the key size specified by the user is valid or not.
It is a good practice to use both these functions whenever you are using RSA encryption.
RSACryptoServiceProvider.Encrypt(): This is the .net inbuilt encryption method to encrypt plain text. This method takes the public key in xml string format only. This was the reason why we converted our public-private key into xml format.To this method we pass the data along with specifying padding or not. Always keep in mind that this method accepts data in byte format.
Decryption functionality:
In code behind file of decrypt button place below code

And in encryption.cs file place below code
The logic is same as encryption but we are using private key for decryption purpose.
Output:
So guys, hopefully you have understood how to code RSA encryption programmatically. In my next blog we will discuss how to use a key generated from Certificate. We will also cover how to convert that generated key into xml string format. Have a nice day.
Bye!!!!!
I will use VS 2012 with .Net framework 4.5 to demonstrate the code.
I am going to make a windows form application for demonstrating purpose. Follow below steps to make windows form application:
1. Open VS 2012
2. Select a new windows application project i.e.(File->New project -> Windows application template) and give it a meaning full name.
Add a class file to the solution and name it as encryption
Import below namespaces in Form.cs and encryption.cs file
- System.Security.Cryptography: This class will provide us the access to utilize inbuilt RSA encryption function.
Now add two buttons and three text boxes to the form. Below is the design of the form
In Form.cs file declare the variables that are required. Please refer below code
Now to generate the public-private key pair use below code. Place this code in encryption.cs file
Let us look at above code what it's exact functionality:
- rsaEncryptionPadding: This is used to specify whether we required padding in the data which we need to encrypt.We will see the significance of this Boolean variable below.
- RSACryptoServiceProvider: This class is present inside System.Security namespace. This class help us to generate both public and private key.
- keySize: This is size of the key defined by the user. It can be 128 or 256 bytes.We will see below what maximum key size can be assign to RSACryptoServiceProvider class
- publicKey: This variable is used to hold the public key in xml string format
- privateKey: This variable is used to hold the private key in xml string format.
We will discuss why it is in xml string format below ?
Public key:
Private Key:
In code behind file of encryption button paste below code:
In above code we are just taking the input from the user and storing it in plaintext variable. Then we are calling GenerateKeys method by passing it the above shown parameters. This method generates the public-private key for us. Then with the help of public key we are calling the Encrypt function present in encryption.cs file.
Note: You can place all these code inside the form.cs file also. I have separated it to make code more understandable.
Encryption Functionality:
Place the below code in encryption.cs file
Let us take a look at above code:
GetMaxDataLength(): This function is used to check the length of the data after it is converted into bytes. The maximum bytes which RSA support is 245 bytes if padding is not applied or else 215 bytes if padding is applied. We have specified above the padding variable.
IsKeySizeValid(): This function is used to check whether the key size specified by the user is valid or not.
It is a good practice to use both these functions whenever you are using RSA encryption.
RSACryptoServiceProvider.Encrypt(): This is the .net inbuilt encryption method to encrypt plain text. This method takes the public key in xml string format only. This was the reason why we converted our public-private key into xml format.To this method we pass the data along with specifying padding or not. Always keep in mind that this method accepts data in byte format.
Decryption functionality:
In code behind file of decrypt button place below code

And in encryption.cs file place below code
The logic is same as encryption but we are using private key for decryption purpose.
Output:
So guys, hopefully you have understood how to code RSA encryption programmatically. In my next blog we will discuss how to use a key generated from Certificate. We will also cover how to convert that generated key into xml string format. Have a nice day.
Bye!!!!!
very good, simple to understand
ReplyDelete