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.
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
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;
Now take a look at encryption function:
static public byte[] RSAEncrypt(byte[] byteEncrypt, RSAParameters RSAInfo, bool isOAEP)
{
try
{
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAInfo);
encryptedData = RSA.Encrypt(byteEncrypt, isOAEP);
}
return encryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
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)
{
try
{
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAInfo);
decryptedData = RSA.Decrypt(byteDecrypt, isOAEP);
}
return decryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
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
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 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