-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.

G droid
  • 956
  • 5
  • 13
  • 36
  • "JavaScript RSA library" yields plenty of results in any web search engine. – CodeCaster Jul 08 '15 at 12:29
  • Maybe you could make JS proxy, which calls you C# method? – Aleksej Vasinov Jul 08 '15 at 12:30
  • @Aleksej I guess OP wants to perform encryption on the client side to prevent eavesdropping, essentially reinventing HTTPS. – CodeCaster Jul 08 '15 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 – G droid Jul 08 '15 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? – nnnnnn Jul 08 '15 at 12:32
  • @ nnnnnn: Well I would really appreciate to get this function converted into javascript code and i would love to know about the tool as well,, – G droid Jul 08 '15 at 12:34
  • @@ nnnnnn: I am using JocysComJavaScriptClasses, a .NET like javascript libraryand I am generating public and private key pairsa like – G droid Jul 08 '15 at 12:49
  • var _publicKey = rsa.ToXmlString(false); var _privateKey = rsa.ToXmlString(true); – G droid Jul 08 '15 at 12:49

1 Answers1

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).

Community
  • 1
  • 1
Niko
  • 6,133
  • 2
  • 37
  • 49