-2

I am a newbie in Javascript.I want to convert a function written in c# to javascript and do the same functionality it was doing in c#. In that process I came across some online converters like duocode,sharpkit,JSIL,JSC,Script# that can do so but did not work. May be I am committing some mistake while operating

Here is the c# code I want to convert to a javascript function:

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);
        }

Any suggestions /help will be really appreciated.

8
  • "JavaScript RSA library" yields plenty of results in any web search engine. Commented Jul 8, 2015 at 12:29
  • Maybe you could make JS proxy, which calls you C# method? Commented Jul 8, 2015 at 12:30
  • @Aleksej I guess OP wants to perform encryption on the client side to prevent eavesdropping, essentially reinventing HTTPS. Commented Jul 8, 2015 at 12:30
  • @CodeCaster: Actually I am trying to implement rsa encryption(using public key) on sever side and decryption(using private key in javascript ) on client side Commented Jul 8, 2015 at 12:32
  • So is the question "What tool converts C# to JavaScript?", or "How do I convert this function to JavaScript?" And how does your JavaScript have a private key? Commented Jul 8, 2015 at 12:32

1 Answer 1

1

What you want to do is not possible with the code you have. There are ways to convert code from one language to another, but only if the code is simple enough and does not use non-basic external libraries/classes. (i.e. the converter can convert loops or other basic logic).

Your code does not consist of any notable logic (except maybe the for-each loop), but only calls external libraries (rsyctypto et al) to do the actual job. In javascript those are definitely not default libraries, so no automated tool can help you there.

Instead google (use use stackoverflow) to search for a code snippet that does the same thing in javascript: encrypt a data using rsa in javascript (like RSA Encryption Javascript and Decrypt Java).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.