Click here to Skip to main content
11,714,776 members (80,967 online)
Click here to Skip to main content

Asymmetric Encryption in .Net

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

Asymmetric Encryption in .Net

 

Background

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

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.

a) Enter a plain text and click encrypt button. The system will display encrypted text.

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)

 

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.

Here is the code: 

'get the decrypted clear text byte array

PlainTextBytes = MyAsymmetricAlgorithm.Decrypt(CipherTextBytes, True)

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

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

‘Create a RSAParameters type

Dim MyParameters As RSAParameters = New RSAParameters

‘export the key and other algorithm values

MyParameters = MyAsymmetricAlgorithm.ExportParameters(True)

'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)

 

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. 


     

License

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

Share

About the Author

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

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

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

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