none
How private public key encryption works

    Question

  • i heard that in private key and public encryption first client1 encrypted data with private key and then send encrypted data to client2 and client2 decrypt data with public key which client1 send him.
     
    i want to know how client2 encrypt data again  for sending it to client1.

    here one example with private public key encryption

    private static string _privateKey;
    private static string _publicKey;
    private static UnicodeEncoding _encoder = new UnicodeEncoding();
    
    private static void RSA()
    {
      var rsa = new RSACryptoServiceProvider();
      _privateKey = rsa.ToXmlString(true);
      _publicKey = rsa.ToXmlString(false);
    
      var text = "Test1";
      Console.WriteLine("RSA // Text to encrypt: " + text);
      var enc = Encrypt(text);
      Console.WriteLine("RSA // Encrypted Text: " + enc);
      var dec = Decrypt(enc);
      Console.WriteLine("RSA // Decrypted Text: " + dec);
    }
    
    public static string Decrypt(string data)
    {
      var rsa = new RSACryptoServiceProvider();
      var dataArray = data.Split(new char[] { ',' });
      byte[] dataByte = new byte[dataArray.Length];
      for (int i = 0; i < dataArray.Length; i++)
      {
        dataByte[i] = Convert.ToByte(dataArray[i]);
      }
    
      rsa.FromXmlString(_privateKey);
      var decryptedByte = rsa.Decrypt(dataByte, false);
      return _encoder.GetString(decryptedByte);
    }
    
    public static string Encrypt(string data)
    {
      var rsa = new RSACryptoServiceProvider();
      rsa.FromXmlString(_publicKey);
      var dataToEncrypt = _encoder.GetBytes(data);
      var encryptedByteArray = rsa.Encrypt(dataToEncrypt, false).ToArray();
      var length = encryptedByteArray.Count();
      var item = 0;
      var sb = new StringBuilder();
      foreach (var x in encryptedByteArray)
      {
        item++;
        sb.Append(x);
    
        if (item < length)
          sb.Append(",");
      }
    
      return sb.ToString();
    }
    here client1 encrypt data with publicKey and client2 has to decrypt it with private key. why should i send my private key to client2?

    so please tell me the actual story with a example. thanks
    Thursday, April 20, 2017 12:21 PM

Answers

  • Hi Mou_kolkata,

    Client 1 has public key 1 and private key 1. Client 2 has public key 2 and private key 2.

    Client 1 send the public key 1 to Client 2. Client 2 send the public key 2 to Client1.

    Client 1 encrypt the data1 with the public key 2 and then pass the data1 to Client 2. Client 2 receive the date1 and decrypt the data1 with private key 2. And then Client2 encrypt the response with public key 1 and then pass the response to Client 1. Client 1 decrypt the response with private key 1.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, April 24, 2017 11:02 AM
    Friday, April 21, 2017 9:42 AM
    Moderator
  • A common analogy is that of a mailbox with a slot. Think of the mailbox as the "public key". Anybody can put a message in the box. It doesn't matter who you give the mailbox to.

    But only the person who owns the box has the right key (the "private key") to unlock it and take out the message.

    Keeping with this analogy:

    If client 1 needs to send data to client 2 securely, client 2 first has to send client 1 his personal mailbox. Client 1 then drops his message in the box and sends it back. Only client 2 has the key to the mailbox (since he owns it) and so only he can open the mailbox and retrieve the message.

    If client 2 wants to send a reply then you obviously have to repeat this process in reverse - i.e. client 1 first has to send client 2 his own personal mailbox to which only he has the key. Client 2 pops his reply into Client 1's mailbox and sends it back. Client 1 then unlocks his own mailbox to take out client 2's reply.


    • Edited by RJP1973 Friday, April 21, 2017 9:55 AM
    • Marked as answer by Mou_kolkata Monday, April 24, 2017 11:03 AM
    Friday, April 21, 2017 9:54 AM
  • Hi Mou,

    You could look into the below article as it explains the process step-by-step with pictures. Hope this helps you.

    RSA Algorithm With C#


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Mou_kolkata Monday, April 24, 2017 11:03 AM
    Friday, April 21, 2017 12:02 PM
    Moderator

All replies

  • The private key is for DEcrypting and is NEVER sent anywhere. The public key is for ENcrypting.

    If you want to send a secure message to another person, you basically ask the other person to generate the keys and send you the public key for encrypting. He keeps the private key.

    You then use the public key to encrypt your message and send it to him. He can then decrypt with his private key.

    If the other person wants to send you a reply you basically follow the same pattern in reverse.

    This is explained here

    Public/private key encryption is an example of asymmetric encryption (different keys used for encrypting and decrypting).

    This can be computationally expensive, so it is worth noting that asymmetric encryption is often used just to securely send the other person a symmetric key, which you then use to encrypt your real message (which the other person decrypts with the same symmetric key (hence "symmetric").

    Thursday, April 20, 2017 1:00 PM
  • Hi Mou_kolkata,

    Thank you for posting here.

    For your question, private key is not used to send to other people generally. Public key is public. You could give the public key to anyone you want. But the private key is private. It only used by yourself. 

    In general, we use public key to encrypt the date and use private key to decrypt the data.

    If you want to use public key and private key to encrypt data, please do not send the private key to client 2.

    For example, client 1 has the public key and private key. Client 1 send the public key to client 2 use the public key to encrypt the data and then pass the data to client 1. Client 1 use the private key to decrypt the data.

    For security, anyone could use the public key to encrypt the data, but only client 2 have private key to decrypt the data.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 21, 2017 8:24 AM
    Moderator
  • sorry not very clear.

    suppose client1 first time need to send some data securely to client2 and client2 need to decrypt and send back response securely to client1.

    so would u please tell me how client1 will encrypt data which client2 will decrypt and how client2 will send encrypted data to client1 which client1 has to decrypt ?

    please explain it like a story.

    Friday, April 21, 2017 9:32 AM
  • Hi Mou_kolkata,

    Client 1 has public key 1 and private key 1. Client 2 has public key 2 and private key 2.

    Client 1 send the public key 1 to Client 2. Client 2 send the public key 2 to Client1.

    Client 1 encrypt the data1 with the public key 2 and then pass the data1 to Client 2. Client 2 receive the date1 and decrypt the data1 with private key 2. And then Client2 encrypt the response with public key 1 and then pass the response to Client 1. Client 1 decrypt the response with private key 1.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, April 24, 2017 11:02 AM
    Friday, April 21, 2017 9:42 AM
    Moderator
  • A common analogy is that of a mailbox with a slot. Think of the mailbox as the "public key". Anybody can put a message in the box. It doesn't matter who you give the mailbox to.

    But only the person who owns the box has the right key (the "private key") to unlock it and take out the message.

    Keeping with this analogy:

    If client 1 needs to send data to client 2 securely, client 2 first has to send client 1 his personal mailbox. Client 1 then drops his message in the box and sends it back. Only client 2 has the key to the mailbox (since he owns it) and so only he can open the mailbox and retrieve the message.

    If client 2 wants to send a reply then you obviously have to repeat this process in reverse - i.e. client 1 first has to send client 2 his own personal mailbox to which only he has the key. Client 2 pops his reply into Client 1's mailbox and sends it back. Client 1 then unlocks his own mailbox to take out client 2's reply.


    • Edited by RJP1973 Friday, April 21, 2017 9:55 AM
    • Marked as answer by Mou_kolkata Monday, April 24, 2017 11:03 AM
    Friday, April 21, 2017 9:54 AM
  • Hi Mou,

    You could look into the below article as it explains the process step-by-step with pictures. Hope this helps you.

    RSA Algorithm With C#


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Mou_kolkata Monday, April 24, 2017 11:03 AM
    Friday, April 21, 2017 12:02 PM
    Moderator
  • it means if client1 do not share private key with client2 then asymmetric encryption will not be possible ?

    for asymmetric encryption both parties need two keys.....am i right ?

    thanks

    Monday, April 24, 2017 11:02 AM