ItayM ItayM - 1 year ago 142
Javascript Question

import .net public xml key to javascript in RSA

I have public key in XML that created in C#
the format is:

<Modulus>rYDd94biRAJ1P6j68xjARl......and so on</Modulus>

I want to create this key in Javascript, and encrypt string with it,
how can I do it?


Answer Source

After so many hours researching, testing, giving up, researching again, etc., for a solution on RSA interoperability for .NET and Javascript, I have found this library:

Their classes have the same working code as the RSACryptoServiceProvider on the server.

What you have to do:

  1. Download the source code from their SVN (user guest, empty password):

  2. Rename the files from *.debug.js to *.js. See the list of files below, on (3).

  3. Add the required files to your html page

    <script type="text/javascript" src="js/System.js"></script>
    <script type="text/javascript" src="js/System.IO.js"></script>
    <script type="text/javascript" src="js/System.Text.js"></script>
    <script type="text/javascript" src="js/System.Convert.js"></script>
    <script type="text/javascript" src="js/System.BigInt.js"></script>
    <script type="text/javascript" src="js/System.BitConverter.js"></script>
    <script type="text/javascript" src="js/System.Security.Cryptography.js"></script>
    <script type="text/javascript" src="js/System.Security.Cryptography.SHA1.js"></script>
    <script type="text/javascript" src="js/System.Security.Cryptography.HMACSHA1.js"></script>
    <script type="text/javascript" src="js/System.Security.Cryptography.RSA.js"></script>
  4. Add this javascript code to encrypt your text:

    function RsaEncrypt()
        //a public key generated from their sample.
        //your should generate yours and stamp it here.
        var xmlParams =
        "<RSAKeyValue>" +
            "<Modulus>pxtmFnrGI6Sb8ziyY+NRUDuQ4b/ETw5WabQ4daFQqzsCEr/6J/LLBU/2D5mO5/Wu5U/Rya1E55aYFZeaZMNqAw==</Modulus>" +
            "<Exponent>AQAB</Exponent>" +
            "<P>2TsVXWPEvDIJv/gd2rX9k0UOyXuaYgoAchIH6vUicis=</P>" +
            "<Q>xO4+OYREQfqYRQK7y73+RaUG0IxobT0OQ0c+Ok2hc4k=</Q>" +
            "<DP>K7/xgpiIU9rECeyfnp/OjS14V+3T3vDivBaTj6eFI3c=</DP>" +
            "<DQ>K4N9ClZ4gp+tn6oP9t//XEIvtEsiE+kmyqTmUhmvMAk=</DQ>" +
            "<InverseQ>p7o4BOlKZQZ693R1ViZ66y5gTjUkNNTd2za7/1YGBCs=</InverseQ>" +
            "<D>XZqFVrYy4qhECruJgVZFp/GVuD5Y0gev88nVjl5r911QT+I8vgJSklTso7jTlpMtf2oe7UZ0WRWEtgPS3tZn4Q==</D>" +
        var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
        // Encrypt
        var decryptedBytes = System.Text.Encoding.UTF8.GetBytes(text);
        var doOaepPadding = true;
        var encryptedBytes = rsa.Encrypt(decryptedBytes, doOaepPadding);
        var encryptedString = System.Convert.ToBase64String(encryptedBytes);
        return encryptedString;

Look for documentation (link above) on how to generate the private and public keys. The code is quite the same as you would manually write .NET on server.

You can also check their test page:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download