Asymmetric Encryption and Decryption implementation using the RSA algorithm.
In this article, I’m going to present asymmetric encryption and decryption implementation using the RSA algorithm provided by the cryptographic service provider (CSP).
for simplicity let's consider there are only three objects/people involving in this transaction. (for this application, I implement one-direction transactions only)
- Bob (Reciever)
- Alice (Sender)
- Eve (Eavesdropper)
To make conversation between Bob and Alice private, they have to use public key sharing approach with asymmetric encryption and decryption
So for the demonstration purpose, I just implemented using.NET 2 Console Applications and Class Library. The overall idea can consider as mainly this includes the following applications.
- Alice Console Application
- Bob Console Application
- Encrypt/Decrypt Service Class Library Application
Also, the key exchange can depict in the following way.
So If
- Alice wants to encrypt something she has to use Bob’s Public Key
- Bob want to encrypt something he has to use Alice’s Public Key
Also if
- Alice wants to decrypt something that Bob encrypted she has to use her Private Key
- Bob wants to decrypt something that Alice encrypted he has to use his Private Key
All keys are different from each other in the RSA algorithm, but mathematically linkage can show the following way.
Alice’s public key → private key
Bob’s public key → private key
As I showed in the first two diagrams for this demonstration I consider Alice as the Sender and Bob as the Receiver
- Alice (Sender ) Console Application
- Bob (Receiver) Console Application
- Encrypt/Decrypt Service Class Library Application
to implement this approach in C# easily we can use RSACryptoServiceProvider
So the implementation in this way.
In order to make this solution work, there is a flow to run this application
- Run Bob Console App to generate Bob’s public/private key pair
2. Run Alice Console App to encrypt Alice message
3. Run Bob Console App to decrypt Alice message
Bob will be disappointed after seeing Alice message, haha :)
In this approach there is a vulnerability, they have to protect their keys in a secure location.
Since Bob’s public key is shared with everyone, Eve also can encrypt a message and send it. to avoid such a situation we have to use the Sign and Verify approach, which is I’m going to explain in my next article.