We sometimes need to hide our confidential data from other users. For that purpose we use encryption algorithms to encrypt our data. There are very many encryption algorithms but I am describing the Rivest, Shamir, Adleman (RSA) Algorithm.
About RSA
RSA is an encryption algorithm.
Developed in: 1977.
Developed by: Ron Rivest, Adi Shamir, and Leonard Adleman.
The RSA algorithm is the most commonly used public key encryption algorithm.
Public Key Encryption
It is also known as asymmetric cryptography.
Two keys are used: Public Key and Private Key.
Public Key: For encryption.
Private Key: For decryption, also known as a secret key.

The preceding diagram show how public key encryption works,
In the diagram 2 users are shown:
First: The Sender (who is sending something by the recipient's Public Key)
Second: The Receiver (who is receiving something from the sender using the private key)
So in a public key cryptosystem, the sender encrypts the data using the public key of the receiver and uses an encryption algorithm that is also decided by the receiver and the receiver sends only the encryption algorithm and public key.
But by using the public key, data can only be encrypted but not decrypted, and the data is only decrypted by the private key that only the receiver has. So no one can hack our data.
In simple terms:
Public Key: Shared with the public that wants to send us data.
Private Key: Kept secret so that when someone sends us data encrypted by our Public Key, we can decrypt the data using the Private Key.
HOW RSA WORKS
Both users (sender and receiver) generates a public and private key.
The following is the procedure for generating a public and private key (see flowchart).
Generation of Public and Private key in RSA
The flowcharts above shows how to generate a public and private key using RSA.
After getting the public and private key the main thing is how to encrypt and decrypt using RSA.
Encryption and Decryption in RSA

Example of RSA: Here is an example of RSA encryption and decryption with generation of the public and private key.
Generate public and private key

Encryption and Decryption

How to use the RSA Algorithm in a C# Windows Forms application
- Open Visual Studio.
- Select "File" -> "New" -> "Project..." or press "Ctrl +Shift +N".
- Now select "Windows Forms application" from the Visual C# templates.
- Now design the Windows Forms form such as follows:

You need to make:
3 TextBoxes for Plain Text, Encrypted Text and Decrypted Text and 2 Buttons.
Now for the coding part.
Coding
1. To use the RSA algorithm in C#, we need to add the following namespace:
- using System.Security.Cryptography;




Latis QueePosted Mar 3, 2023, 4:19 AM
Nice work! I also saw some great cryptographic tools on kimbalite.com
Tin Le VanPosted Oct 13, 2021, 1:33 PM
Can the sender descypt the encrypted message sent to the receiver? The problem is sender encrypted message using receiver publickey. Later, sender want to read what have been sent to receiver, he only has the encrypted mesassage, how can he decrypt it? The problem is, orginal message was not saved because security reason, only encrypted message was saved.
RomiPosted Sep 22, 2021, 9:48 AM
Can you share where we can pass public and private key of our own
RenuPosted Jun 21, 2021, 7:35 PM
Thank you very much for the awesome code. However, I have encrypted data and public/private keys generated by php code. I am not able to figure out how to pass the string public key to the decrypt function. Could you please help? Thanks
mayurPosted Sep 29, 2020, 7:16 AM
I have generated Public and private key using RSA and stored that in web config. Then i have created RSAParameters object and pass Modulus and Exponent as a public key to method and text encrypted, But while decryption i am getting error The parameter is incorrect. I am sending RSAParameter object to decryption method with all Modulus, exponent,P,Q,DP,DQ,InverseQ,D.. Can you assist where i am doing wrong.
Sourabh SomaniPosted Sep 12, 2020, 12:20 PM
Sd;asdasdasd
Mani RaviPosted Feb 20, 2020, 10:10 PM
How to generate keys in my own
dosan azaPosted Feb 13, 2020, 5:03 AM
You are brilliant . Thanks from KZ
Brainy EdetPosted Jul 22, 2019, 4:35 AM
Please why not combining the codes instead of posting it in different segment, am having trouble putting them together
Anastasia hPosted Jun 3, 2019, 7:09 AM
I want to know, why when i encrypted one plain text and save the cipher and when i encrypted another and changed the cipher with previous cipher the decrypted text is the same as in first text-box (second encrypted text), even if i changed the cipher?
Saira ShahidPosted Nov 22, 2018, 8:25 AM
I want to know why it is giving direct converting the cipher text into plain text on Decrypt button? should not it asked to enter the private key first and then conversion should be done??
swapnali morePosted Jul 6, 2018, 2:34 AM
I need to know why the encrypted text appear in Chinese form like "????????????????????????????????????????????????????????????????". Cant it be simple alpha numeric string.
swapnali morePosted Jul 3, 2018, 7:15 AM
I need to use public key for encryption and user will have its own private key to decrypt the data and on the form the data will be preseneted in encrypted form and one button will be there to decrypt the data and user will enter its own private key can you help me for the same
athr alghamdiPosted Apr 16, 2018, 4:08 PM
How can i show they key size to the user? this is very important.
Ngơ NgáoPosted Apr 11, 2018, 7:53 AM
Could you please give me the full code of this program?
Dev ConceptPosted Apr 5, 2018, 2:15 AM
Hi Sourabh,thank you for detail explain RSA. Please Explain the Parameter "RSAKey", which is used in Encryption Funtion and Decryption Function, with the help of above Method "Generating of Public and Private keys".
Jayashree BodkhePosted Jan 18, 2018, 10:06 AM
Can we encrypt data first by sender's private key & then by receiver's public key.Please provide code
Abdul MarwatPosted Apr 13, 2017, 2:24 PM
Please Explain the Parameter "RSAKey" which is used in Encryption Funtion and Decryption Function, with the help of above Method "Generating of Public and Private keys" , by selecting any 2 prime numbers
Former memberPosted Apr 13, 2017, 7:08 AM
Where is the public and private key?
Vasile DiaconuPosted Nov 8, 2015, 3:13 PM
how to get normal letters instead of chiness
Vaikesh K PPosted Oct 5, 2015, 8:48 AM
Good one
Ajeet MishraPosted Sep 30, 2015, 2:26 AM
great
Gowtham RajamanickamPosted Apr 6, 2015, 4:14 AM
great
vasantha meenaPosted Jan 3, 2015, 6:21 AM
Really crystal clear article for RSA. Thanks i try and learned many things..
Afraz Nazeer JanjuaPosted Nov 17, 2014, 12:47 PM
thanks good please implement RSA in FLV
Long ChâuPosted Oct 30, 2014, 9:50 AM
thank you, i'm totally stuck with this crazy RSA and you open my mind :)
AL GalibPosted Sep 8, 2014, 2:24 AM
how i calculate run time for encryption & decryption? plzz help
AL GalibPosted Sep 8, 2014, 2:24 AM
how i calculate run time for encryption & decryption
Bosco RomeroPosted Jun 16, 2014, 8:12 AM
you work is really very good,but when i check your RSA apps, i was encrypt two original messages separately, when decrypt first message cipher text was given second original message.
Eman Bany SalamehPosted May 9, 2014, 12:13 PM
I tried this code but it doesn't show the encrypted text and the decrypted text in the textboxes
john homesPosted Apr 23, 2014, 3:59 AM
its urgent I really need to know languages used in encrypted text because when you save it in notepad it shows null character
john homesPosted Apr 23, 2014, 3:58 AM
please spcefy languages of encrypted text
HJFKd jvkdsjpoPosted Jan 31, 2014, 9:22 AM
PLease post the version that works on other machines too.
Keyur PatelPosted Jan 7, 2014, 2:13 AM
Please try this encrypt/decrypt algorithm on live project and deploy it. I think, you might get an error because asymmetric key is stored on machine level config, hence it wouldn't decryption on other machine. Please clear me if i am wrong.
Shaili DashoraPosted Dec 11, 2013, 5:14 AM
Nice article