• 0

[C#] Can't send public key to client.


Question

I have been using encryption for a bit now with no problem. However when trying to make a test windows 8 app I noticed I can't send the public key back to the client to send encrypted data back to the server. It always throws an exception. Here is my code:

private async void TestFunc()
        {
            TestEncSvcClient client = new TestEncSvcClient();
            string msg = "Test message (enc server).";
            byte[] privateKeyBlob = await client.GetPrivateKeyAsync();
            // Now that we have the key lets attempt to encrypt/decrypt with it.
            AsymmetricKeyAlgorithmProvider rsaP = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
            CryptographicKey key = rsaP.ImportPublicKey(privateKeyBlob.AsBuffer());
            byte[] encMsg = CryptographicEngine.Encrypt(key, Encoding.UTF8.GetBytes(msg).AsBuffer(), null).ToArray();
            this.xMainTxtBlk.Text += "\n\nReceived key from server. Encryption length: " + encMsg.Length + "\nEncrypted version: " + Encoding.UTF8.GetString(encMsg, 0, encMsg.Length);
            // Now lets decrypt it.
            //byte[] decMsg = CryptographicEngine.Decrypt(key, encMsg.AsBuffer(), null).ToArray();
            //this.xMainTxtBlk.Text += "\n\nDecrypted version: " + Encoding.UTF8.GetString(decMsg, 0, decMsg.Length);
        }

Service code

public class TestEncSvc : ITestEncSvc
    {
        byte[] ITestEncSvc.GetPrivateKey()
        {
            byte[] privateBlob = null;
            using (RSACryptoServiceProvider rsaP = new RSACryptoServiceProvider())
            {
                privateBlob = rsaP.ExportCspBlob(false);
            }
            return privateBlob;
        }
    }

I get this error: ASN1 bad tag value met. (Exception from HRESULT: 0x8009310B). No one at Microsoft's forums seem to be answering others questions similar to what I am asking so I am turning to neowin.net in hopes to get an answer (this post is someone asking a similar question: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/204e55c3-d0c3-4736-bdb7-1ac41f62fd54 however I am just using the cspblob exported in this example, haven't tried the xml version yet). I'm going to post one myself to see if I get any luck.

Link to comment
https://www.neowin.net/forum/topic/1115837-c-cant-send-public-key-to-client/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

So I've tried this link: http://social.msdn.microsoft.com/Forums/en-AU/winappswithcsharp/thread/4b96bcd5-39bd-4bd8-85d4-bf689e92e46a to no avail. Still throws a bad data tag error. After a solid week of nothing I have to say it's making me bitter. How could Microsoft not have documentation on this? People have WCF services, show how to interact with them.

This topic is now closed to further replies.