In previous Article Ã¢â‚¬â€œ

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

System.Security.Cryptography

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)
{
try
{
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.
RSA.ImportParameters(RSAInfo);
//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)
{
Console.WriteLine(e.Message);
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)
{
try
{
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.
RSA.ImportParameters(RSAInfo);
//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)
{
Console.WriteLine(e.ToString());
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**

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!

## Also Read My Related Article :

cryptography--symmetric-encryption-by-symmetric.aspx Part I

cryptography--symmetric-encryption-by-symmetric.aspx Part 2

## 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Ã¢â‚¬Â.