Click here to Skip to main content
12,394,573 members (74,049 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked

Cryptography : Asymmetric Encryption by using Asymmetric Algorithm Classes.

, 28 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This Blog is enable you to understand the basic of Cryptography with Asymmetric Encryption Algorithm Classes.
In previous blog – Symmetric Encryption by Symmetric Algorithm Classes–Part 1 and Part 2 we have learned about basic introduction of Cryptography based on Symmetric Encryption. so, now In addition to previous blog here we will learn about basics of Asymmetric Encryption.

Asymmetric Encryption :

Asymmetric encryption is also referred to as public key encryption because it uses public key as well as private key. means Its having a secret key that must be kept from unauthorized or anonymous users and a public key that can make public to any one. Hence we can say that Asymmetric encryption is designed so that the private key remains shield and secret, whereas the publick key is widely distributed. The private key is used to lock information, whereas the public key is used to unlock it.
The main benefit of opting asymmetric encryption is that you can share encrypted data without having access to the private key. In standard mode asymmetric encryption is used more commonly than symmetric encryption, and it is proved that its a standard used to help secure communication over the Internet.
Have a look at animated pic to view its working

Animated Pic:- Asymmetric Encryption Decryption Concept

we came to know in earlier blog about symmetric encryption, the same key is used for both encryption and decryption, however this approach is simpler but less secure since the key must be communicated to and known at both sender and receiver locations.
Example For Better Understand :
Lets assume about conversion of Plain text between A and B. If ‘A’ send message to ‘B’, ‘A’ can find out public key (but not private key of ‘B’) of ‘B’ from a central administrator and encrypt a message to ‘B’ using ‘B’ public key. When ‘B’ receive it, ‘B’ can decrypt it with ‘B’ private key. In addition to encrypting messages (which can ensures privacy), ‘B’ can authenticate itself to ‘A’ (so ‘A’ know that it is really ‘B’ who is the sender of message to ‘A’) by using ‘B’ private key to encrypt a digital certificate. When ‘A’ receive it, ‘A’ can use ‘B’ public key to decrypt it.

Asymmetric Algorithm Classes


namespace provides encryption classes that provide most popular Asymmetric algorithms like

  • RSA and RSACryptoServiceProvider
  • DSA and DSACryptoServiceProvider

RSACryptoServiceProvider Class

RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it in 1977-[ From Wikipedia ]

The RSA class is an abstract class that extends the Asymmetric Algorithm class and provides support for the RSA algorithm. The .NET Framework RSA algorithm support an encryption key size ranging from 384 bits to 16,384 bits in increments of 8 bits by using the Microsoft Enhanced Cryptographic Provider and an encryption key size ranging from 384 bits to 512 bits in increments of 8 bits by using the Microsoft Base Crystallographic Provider.

The RSACryptoServiceProvider class exnteds the RSA class and is the concrete RSA algorithm class.

Implementation of RSACryptoServiceProvider Class

To perform Encryption and Decryption. you must add

using System.Security.Cryptography; // Namespace

Now take a look at encryption function

static public byte[] RSAEncrypt(byte[] byteEncrypt, RSAParameters RSAInfo, bool isOAEP)
               byte[] encryptedData;
               //Create a new instance of RSACryptoServiceProvider.
               using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())

                   //Import the RSA Key information. This only needs
                   //toinclude the public key information.

                   //Encrypt the passed byte array and specify OAEP padding.
                     encryptedData = RSA.Encrypt(byteEncrypt, isOAEP);
               return encryptedData;
           //Catch and display a CryptographicException
           //to the console.
           catch (CryptographicException e)

               return null;


In above code Encrypt Function is used to encrypt plain text to cipher text. Encrypt Function Need two parameter first one is byte array of plain text and second one specify OAEP padding(True or False)

Now in same way we need to create function for Decrypt the PlainText(Encrypted Text)
have a look at given function which is responsible to decrypt encrypted text.

static public byte[] RSADecrypt(byte[] byteDecrypt, RSAParameters RSAInfo, bool isOAEP)
              byte[] decryptedData;
              //Create a new instance of RSACryptoServiceProvider.
              using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                  //Import the RSA Key information. This needs
                  //to include the private key information.

                  //Decrypt the passed byte array and specify OAEP padding.
                    decryptedData = RSA.Decrypt(byteDecrypt, isOAEP);
              return decryptedData;
          //Catch and display a CryptographicException
          //to the console.
          catch (CryptographicException e)

              return null;

we can see in above code Decrypt Function is used in same manner to decrypt cipher text to plain text. Decrypt Function Need two parameter first one is byte array of encrypted text and second one specify OAEP padding(True or False)

Note :- OAEP padding is only available on Microsoft Windows XP or later.

Now we have created function, so we can use both function to appropriate manner to accomplishment of Encryption Decryption task.

Note : we need to access RSACryptoServiceProvider class here.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

How to use Encrypt and Decryption Function

Note : below code is tested in Windows Application. You can Download Source Code for better understand.

UnicodeEncoding ByteConverter = new UnicodeEncoding();
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        byte[] plaintext;
        byte[] encryptedtext;

For Encrypt Text

plaintext = ByteConverter.GetBytes(txtplain.Text);
           encryptedtext = RSAEncrypt(plaintext, RSA.ExportParameters(false), false);
           txtencrypt.Text = ByteConverter.GetString(encryptedtext);

For Decrypt Text or Back to Plain Text

//While Decryption set True,  the private key information (using RSACryptoServiceProvider.ExportParameters(true),
byte[] decryptedtex = RSADecrypt(encryptedtext, RSA.ExportParameters(true), false);
 txtdecrypt.Text = ByteConverter.GetString(decryptedtex);

What we’ve seen

  • Create Function of Encryption and Decryption
  • Create Encoder
  • Create RSACryptoServiceProvider Instance.
  • Create byte array to illustrate the encrypted and decrypted data.
  • encrypted text and Display Cipher text
  • Decrypt cipher text and Display back to plain text.

Output by using RSAEncryption Program.

Animated Pic : Screen Shot of RSA Encryption Program.

Download Source Code :

Want to Download Source Code : Click Me!

Further Reading

RSA Algorithm-[Wikipedia]
RSACryptoServiceProvider Class-[MSDN]
Data Confidentiality-[MSDN]

Coming Next
Please Stay in touch for extend part of this article. Topic will move around “Introduction and Implementation of DSACryptoServiceProvider for Beginners”.

Filed under: .Net, C#, CodeProject, Cryptography Tagged: .Net, C#, CodeProject, Cryptography


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer

An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.

He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..

He keep himself on the desk of his imagination, hanging around with some inceptions.

You may also be interested in...

Comments and Discussions

Questionusing existing third party public key for Encryption Pin
VishalHealthcare1-Jun-13 7:30
memberVishalHealthcare1-Jun-13 7:30 
GeneralMy vote of 5 Pin
osmofrilander5-Sep-12 22:02
memberosmofrilander5-Sep-12 22:02 
GeneralRe: My vote of 5 Pin
RaviRanjankr7-Sep-12 2:45
mvpRaviRanjankr7-Sep-12 2:45 
GeneralMy vote of 5 Pin
Christian Amado29-Aug-12 11:05
memberChristian Amado29-Aug-12 11:05 
GeneralRe: My vote of 5 Pin
RaviRanjankr30-Aug-12 7:34
mvpRaviRanjankr30-Aug-12 7:34 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 29 Aug 2012
Article Copyright 2012 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid