0

I have been stuck at this problem for almost two days now and I don't seem to find any solution. I have hosted a WCF service on my machine that contains a method SendCredentials which accepts two string parameters.

Now I am supposed to send a public key to my service through which it will do encryption(Asymetric cryptography) and send some information back to the client.

I am not able to pass that public key to the service method from the client as it is in XML format.Here is my client side code:

    $(document).ready(function () {
        $("#btnSend").click(function () {
            debugger;
            jQuery.support.cors = true;

            var doOaepPadding = true;
            var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
            _privateKey = rsa.ToXmlString(true);
            _publicKey = rsa.ToXmlString(false);
            var data = $("#txtName").val();
            var name = "testvalue";

            var _privateKey = rsa.ToXmlString(true);
            **var _publicKey = rsa.ToXmlString(false);**

            //<![CDATA[ and ]]>;
            $.ajax({

                type: "POST",

                url: 'http://localhost:51348/TestService.svc/SendCredentials',
               crossDomain: true,

                   data:JSON.stringify({ mac: "bac", pubKey: _publicKey }),

                contentType: "application/json",
                dataType: "json",

                success: function (result) {
                    var ans = JSON.stringify(result);

                    alert(ans);

                    //   result = new XMLSerializer().serializeToString(result.documentElement);

                },
                error: function (xhr, err) {
                    alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                    alert("responseText: " + xhr.responseText);
                }
            });
            return false;

        });

    });

</script>

_publicKey is the variable I want to pass but throws above said error. Any suggestions How do i pass this XML variable would be really appreciated.

G droid
  • 956
  • 5
  • 13
  • 36

1 Answers1

0

I would suggest you to convert _publicKey to base64 string

convert your _publicKey to string then byte Array and use

Convert.ToBase64String(byte[] inArray)

and on the service side do the reverse

System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(endodedString), true));
Thakur
  • 559
  • 6
  • 15
  • please see this [link]http://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript – Thakur Jul 06 '15 at 11:23
  • @ Anand Thakur: No that did not work...Is there any other public key exchange mechanism I can implement in javascript..where public key is not generated in XML?? – G droid Jul 06 '15 at 12:33
  • I think this should help you [link]http://stackoverflow.com/questions/1002179/how-can-i-pass-windows-authentication-to-webservice-using-jquery – Thakur Jul 10 '15 at 05:10