Click here to Skip to main content
15,867,835 members
Articles / Operating Systems / Windows
Article

Asymmetric Encryption in .Net

Rate me:
Please Sign up or sign in to vote.
1.00/5 (10 votes)
25 Aug 20063 min read 45.4K   275   9   14
A sample .Net application that implements asymmetric encryption

Asymmetric Encryption in .Net<o:p>

<o:p> 

Background<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.

<o:p> 

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.

<o:p> 

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.

<o:p> 

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.

<o:p> 

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>

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


     

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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