Click here to Skip to main content
14,490,767 members

Cryptography: Asymmetric Encryption by Using Asymmetric Algorithm Classes

Rate this:
4.95 (12 votes)
Please Sign up or sign in to vote.
4.95 (12 votes)
28 Aug 2012CPOL
This blog will enable you to understand the basic of Cryptography with Asymmetric Encryption Algorithm Classes.
In the 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 the 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. This means it's 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 public 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 for 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 it's a standard used to help secure communication over the Internet.
Have a look at the animated pic to view its working.

Image 1

Animated Pic: Asymmetric Encryption Decryption Concept

We came to know in the 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 Understanding

Let's 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 the above code, Encrypt Function is used to encrypt plain text to cipher text. Encrypt function needs two parameters, first one is byte array of plain text and second one specifies OAEP padding(True or False).

Now in the same way, we need to create function for decrypting the PlainText(Encrypted Text). Have a look at the given function which is responsible for decrypting 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 the above code, Decrypt function is used in the same manner to decrypt cipher text to plain text. Decrypt Function needs two parameters, the first one is byte array of encrypted text and the second one specifies 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 functions to the appropriate manner to accomplish Encryption Decryption task.

Note: We need to access RSACryptoServiceProvider class here.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

How to Use Encrypt and Decryption Function

Note: The below code is tested in Windows Application. You can download the source code for better understanding.

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

Image 2

Animated Picture: Screenshot of RSA Encryption Program

Further Reading

Coming Next

Please stay in touch for the extended part of this article. The topic will move around “Introduction and Implementation of DSACryptoServiceProvider for Beginners”.

Filed under: .NET, C#, CodeProject, Cryptography
Tagged: .NET, C#, CodeProject, Cryptography
Image 3 Image 4


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
No Biography provided

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
oskifree5-Sep-12 22:02
professionaloskifree5-Sep-12 22:02 
GeneralRe: My vote of 5 Pin
RaviRanjanKr7-Sep-12 2:45
professionalRaviRanjanKr7-Sep-12 2:45 
GeneralMy vote of 5 Pin
Christian Amado29-Aug-12 11:05
professionalChristian Amado29-Aug-12 11:05 
GeneralRe: My vote of 5 Pin
RaviRanjanKr30-Aug-12 7:34
professionalRaviRanjanKr30-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.

Technical Blog
Posted 28 Aug 2012


22 bookmarked