Click here to Skip to main content
13,290,556 members (54,474 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 25 Aug 2006

Asymmetric Encryption in .Net

, 25 Aug 2006
Rate this:
Please Sign up or sign in to vote.
A sample .Net application that implements asymmetric encryption

Asymmetric Encryption in .Net<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>



Asymmetric encryption allows conversion of simple text to cipher text (encrypted text) and vice versa. The key used for encryption is different from the key used for decryption. The keys together are known as the key pair.  Typically one key is kept private and the other key is made public. A message encrypted with one’s public key can only be decrypted with his private key. This allows secure communication of messages. For example, a sender can encrypt their message using the recipient’s public key. The message can only be decrypted by the recipient because the recipient’s private key is only known only to the recipient.


The Project<o:p>

This code project shows how the asymmetric encryption can be used to encrypt and decrypt a string. It also displays how to extract and display the public and private keys. The project is organized as a simple VB.Net windows forms project with one form. The form controls are self explanatory.


The project requires adding a reference to System.Security assembly. The .Net framework supports only the RSA asymmetric encryption and the main class that performs this function is RSACryptoServiceProvider. This project instantiates an object of this type using the default constructor. The runtime creates this class with a random key pair and uses the same for encrypting and decrypting messages. For displaying encrypted string, I have used ‘Convert.ToBase64String’ function. This is to make sure the encrypted characters are human-readable. Converting characters to base 64 string has nothing to do with encryption or decryption.


Another important point to note is that there are very few lines of code that are required for the encryption or decryption. .Net classes are wrappers for the windows crypto API that was introduced with Windows 2000. All newer operating systems include this API.


Even though this is self-explanatory, the application can be tested as follows.<o:p>

a) Enter a plain text and click encrypt button. The system will display encrypted text.<o:p>

Here is the code: 

'get the clear text and convert it to byte array using UTF8 or the default encoding format
PlainTextBytes = System.Text.Encoding.UTF8.GetBytes(TextBoxOriginal.Text)
'encrypt the text
CipherTextBytes = MyAsymmetricAlgorithm.Encrypt(PlainTextBytes, True)
'base64 encode the ciphertext just for display purpose
TextBoxEncrypted.Text = Convert.ToBase64String(CipherTextBytes)<o:p>


b)      To test decryption, you should delete the text in the original textbox and click decrypt button. This should fill the original textbox with the text that you initially entered.<o:p>

Here is the code: 

'get the decrypted clear text byte array<o:p>

PlainTextBytes = MyAsymmetricAlgorithm.Decrypt(CipherTextBytes, True)<o:p>

'convert the byte array to text using the same encoding format that was used for encryption
TextBoxOriginal.Text = System.Text.Encoding.UTF8.GetString(PlainTextBytes)<o:p>

c)      To view the public and private keys, you can click Showkeys button anytime.

‘Create a RSAParameters type<o:p>

Dim MyParameters As RSAParameters = New RSAParameters<o:p>

‘export the key and other algorithm values <o:p>

MyParameters = MyAsymmetricAlgorithm.ExportParameters(True)<o:p>

'display the private key. Base64 encode the text to make display easier
TextBoxPrivateKey.Text = Convert.ToBase64String(MyParameters.D)
'display the public key. Base64 encode the text to make display easier
TextBoxPublicKey.Text = Convert.ToBase64String(MyParameters.Modulus)<o:p>


Please note that RSACryptoServiceProvider is defined as an instance variable of the form. So the same instance of the class is used for both encryption and decryption. If you create separate instances of RSACryptoServiceProvider for both encryption and decryption, you’ll get instances with different default key pair and so the decryption will not work. Other option is to programmatically store and set the keys in the RSACryptoServiceProvider instance. We strongly recommend using MyAsymmetricAlgorithm.Clear() to release the resources held by the wrapper RSACryptoServiceProvider instance. 



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
E.Arun14-Sep-09 22:56
memberE.Arun14-Sep-09 22:56 
GeneralRe: My vote of 1 Pin
shekhar_shashi12-Nov-09 15:36
membershekhar_shashi12-Nov-09 15:36 
GeneralMy vote of 1 Pin
project1234qwerty18-Jan-09 16:07
memberproject1234qwerty18-Jan-09 16:07 
Generaldownload location Pin
rajeshwark8-Aug-08 12:42
memberrajeshwark8-Aug-08 12:42 
Questionsource code Pin
mohebi24-May-08 20:05
membermohebi24-May-08 20:05 
Questionproblem in decryption Pin
Jhan Zaib18-Dec-06 3:23
memberJhan Zaib18-Dec-06 3:23 
AnswerRe: problem in decryption Pin
shekhar_shashi19-Dec-06 21:17
membershekhar_shashi19-Dec-06 21:17 
GeneralRe: problem in decryption Pin
Jhan Zaib28-Dec-06 20:16
memberJhan Zaib28-Dec-06 20:16 
Questioncode? Pin
Schmal25-Aug-06 18:46
memberSchmal25-Aug-06 18:46 
AnswerRe: code? Pin
shekhar_shashi26-Aug-06 7:28
membershekhar_shashi26-Aug-06 7:28 
AnswerRe: code? Pin
shekhar_shashi26-Aug-06 7:33
membershekhar_shashi26-Aug-06 7:33 
AnswerDownload location Pin
aleale220-Sep-06 6:00
memberaleale220-Sep-06 6:00 
QuestionRe: Download location Pin
project1234qwerty18-Jan-09 16:09
memberproject1234qwerty18-Jan-09 16:09 
GeneralRe: Download location Pin
nabil91118-Oct-12 2:23
membernabil91118-Oct-12 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 25 Aug 2006
Article Copyright 2006 by shekhar_shashi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid