Asymmetric Encryption and Decryption implementation using the RSA algorithm.

Kelum
3 min readJun 19, 2021

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)

  1. Bob (Reciever)
  2. Alice (Sender)
  3. Eve (Eavesdropper)
Alice/Eve/Bob

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.

  1. Alice Console Application
  2. Bob Console Application
  3. 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
Encryption

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
Decryption

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

  1. Alice (Sender ) Console Application
  2. Bob (Receiver) Console Application
  3. Encrypt/Decrypt Service Class Library Application

to implement this approach in C# easily we can use RSACryptoServiceProvider

Overall Solution

So the implementation in this way.

Service Interface Contract
Method Implementation
Alice Program
Bob Program

In order to make this solution work, there is a flow to run this application

  1. Run Bob Console App to generate Bob’s public/private key pair
Encryption File Empty

2. Run Alice Console App to encrypt Alice message

Encryption Success Message

3. Run Bob Console App to decrypt Alice message

Final Decrypted 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.

--

--