asymmetric cryptography

Asymmetric Cryptography (Public Key Cryptography) – Part I

Asymmetric cryptography, also known as public key cryptography, it uses two distinct, yet related keys. One key, the Public Key, is used for encryption and the other, the Private Key, is for decryption.

Let say Mahesh wants to send an encrypted message to Shalini, Mahesh will look for Shalini Public key and use it for encrypt the message before sending it. Then Shalini Can decrypt the message using her related private key.

if Mahesh encrypts the message using his private key, then the message can be decrypted only using Shalini’s public key, thus it will also authenticate Shalini. These encryption and decryption processes happen automatically hence no need to share the keys.

Asymmetric cryptography is slower then symmetric cryptography

1. Does not require to share key
2. Simple structure

RSA is common asymmetric algorithm, I will use the same for this example. To demonstrate, I have used .Net framework in build library “System.Security.Cryptography”.

Here is C# Source code for the same.

using System;
using System.Security.Cryptography;
using System.Text;

namespace AsymmetricCryptography
    public class AsymmetricRsa     
        static void Main()
            var rsaCryptoService = new RSACryptoServiceProvider();

            var publicKey = rsaCryptoService.ToXmlString(false); // false to get the public key   
            var privateKey = rsaCryptoService.ToXmlString(true); // true to get the private key   

            Console.WriteLine("Enter message to encrypt");
            var message = Console.ReadLine();

            var encryptedMessage = EncryptMessage(publicKey, message, rsaCryptoService);
            Console.WriteLine(new UnicodeEncoding().GetString(encryptedMessage));

            var decryptData = DecryptData(privateKey,encryptedMessage);
            Console.WriteLine(new UnicodeEncoding().GetString(decryptData));

        static byte[] EncryptMessage(string publicKey, string message, RSACryptoServiceProvider rsaCryptoService)
            var dataToEncrypt = new UnicodeEncoding().GetBytes(message);
            rsaCryptoService.FromXmlString(publicKey);//We can even create new instance for RSACryptoServiceProvider
            return rsaCryptoService.Encrypt(dataToEncrypt, false);
        static byte[] DecryptData(string privateKey, byte[] dataToDecrypt)
            var rsaCryptoService = new RSACryptoServiceProvider(); //a new instance for receiver;
            return rsaCryptoService.Decrypt(dataToDecrypt, false);

Leave a Reply

Your email address will not be published. Required fields are marked *