Cryptography : Asymmetric Encryption by using Asymmetric 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

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

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

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in .Net, C#, CodeProject, Cryptography and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s