Asymmetric encryption

Asymmetric encryption uses two keys—a public key and a private key. Because of this, it runs bit slowly. Also, it is necessary to keep the private key safe at all times. Unless you have the private key, you cannot decrypt the message.

Now, let's jump into an example and try to understand how this is done. In this scenario, we will be using RSACryptoServiceProvider. This algorithm provides us with public and private keys that can be used to encrypt and decrypt messages. The encrypt method accepts a public key and text to encrypt, and we then convert the text to a byte array since the encrypt method accepts byte arrays. Then, we set the public key for the algorithm and invoke the encrypt method:

public static byte[] EncryptAsymmetric(string publicKey, string texttoEncrypt)
{
byte[] result;
UnicodeEncoding uc = new UnicodeEncoding();
byte[] databytes = uc.GetBytes(texttoEncrypt);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
result = rsa.Encrypt(databytes, true);
}
return result;
}

In the decrypt method, we pass the byte array that needs to be decrypted along with a private key. Once a message is encrypted using a public key, it can only be decrypted using its corresponding private key:

public static string DecryptAsymmetric(string privateKey, byte[] bytestoDecrypt)
{
byte[] result;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKey);
result = rsa.Decrypt(bytestoDecrypt, true);
}
UnicodeEncoding uc = new UnicodeEncoding();
string resultText = uc.GetString(result);
return resultText;
}

The following is the main method, where we create the RSACryptoproviderservice class to get public and private keys. rsa.ToXmlString(false) provides a public key, and setting it to true will give us a private key. We will use these keys to encrypt and decrypt messages:

static void Main(string[] args)
{
#region asymmetric Encryption
Console.WriteLine("Using asymmetric Algorithm");
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKey = rsa.ToXmlString(false);
string privateKey = rsa.ToXmlString(true);
Console.WriteLine("Encrypting data ");
byte[] resultbytes = EncryptDecryptHelper.EncryptAsymmetric(publicKey,"This is a dummy text to encrypt");
Console.WriteLine("Data Encrypted. Press any key to decrypt message");
System.Console.ReadKey();
Console.WriteLine("Decrypting content");
string resultText = EncryptDecryptHelper.DecryptAsymmetric(pricateKey, resultbytes);
Console.WriteLine($"Decrypted data : {resultText}");
#endregion

// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}

Execute the program by changing the input text, or you can try changing the algorithm. However, when you change the algorithm, you may need to apply any syntactical changes that are required in order for the program to run and work:

When you execute the program, you see the preceding output. In the sample, you are using an asymmetric algorithm to encrypt content. As the control flows through the code, it display messages. Once the message is encrypted, it will ask you to press any key. Upon pressing any key, the program decrypts the message and displays it on screen.

In the scenario that we discussed under symmetric algorithms, where you want to perform secure transactions between two parties, you can use public-private key combinations.

A receiver performs encryption using a private key and sends the file or block of text to the receiver, where a public key is used to decrypt the content. In the event the public-private keys do not match, you cannot read or validate the data. This is one way to validate input data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.14.245