Note that there are some explanatory texts on larger screens.

plurals
  1. POImplement secure communication using RSA encryption in C#
    primarykey
    data
    text
    <p>I want to implement a scenario where two endpoints can securely communicate with each other using public/private key encryption. The scenario is following:</p> <p>For A to send a message to B:</p> <blockquote> <p>A encrypts the message using A's private key. </p> <p>A encrypts the message using B's public key.</p> <p>A sends the message. </p> <p>B receives the message.</p> <p>B decrypts the message using A's public key. </p> <p>B decrypts the message using B's private key.</p> <p>B reads the message.</p> </blockquote> <p>Here is what I have in C# using RSA encryption:</p> <pre><code>// Alice wants to send a message to Bob: String plainText = "Hello, World!"; Byte[] plainData = Encoding.Default.GetBytes(plainText); Byte[] cipherData = null; RSACryptoServiceProvider alice = new RSACryptoServiceProvider(); RSACryptoServiceProvider bob = new RSACryptoServiceProvider(); var alicePrivateKey = alice.ExportParameters(true); var alicePublicKey = alice.ExportParameters(false); var bobPrivateKey = bob.ExportParameters(true); var bobPublicKey = bob.ExportParameters(false); RSACryptoServiceProvider messenger = new RSACryptoServiceProvider(); messenger.ImportParameters(alicePrivateKey); cipherData = messenger.Encrypt(plainData, true); messenger.ImportParameters(bobPublicKey); cipherData = messenger.Encrypt(cipherData, true); messenger.ImportParameters(alicePublicKey); cipherData = messenger.Decrypt(cipherData, true); messenger.ImportParameters(bobPrivateKey); cipherData = messenger.Decrypt(cipherData, true); String result = Encoding.Default.GetString(alice.Decrypt(cipherData, true)); </code></pre> <p>Clearly, there is something wrong with the following lines:</p> <pre><code>messenger.ImportParameters(bobPublicKey); cipherData = messenger.Encrypt(cipherData, true); </code></pre> <p>Which throws <em>System.Security.Cryptography.CryptographyException</em> with message <em>{ "Bad Length" }</em>.</p> <p>As I can see it is not able to encrypt the data using just the public part of bob's key.</p> <p>Can someone throw some light on how to properly accomplish what I want to do in <em>C#</em>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload