0

I'm a bit baffled when it comes to converting this javascript over to C#...

Any help would be appreciated!

Here is the javascript:

function d(strInput) {
    strInput = decoder(strInput);
    var strOutput = "";
    var intOffset = (key + 112) / 12;
    for (i = 4; i < strInput.length; i++) {
        thisCharCode = strInput.charCodeAt(i);
        newCharCode = thisCharCode - intOffset;
        strOutput += String.fromCharCode(newCharCode)
    }
    document.write(strOutput)
}

And this is my attempt at converting it to C#. It works some of the time, but most of the time for negative numbers as the key...

public string decode(int key, string data)
{
    int i;
    string strInput = base64Decode(data);
    StringBuilder strOutput = new StringBuilder("");

    int intOffset = (key + 112) / 12;
    for (i = 4; i < strInput.Length; i++)
    {

        int thisCharCode = strInput[i];
        char newCharCode = (char)(thisCharCode - intOffset);
        strOutput.Append(newCharCode);
    }
    return strOutput.ToString();
}

Currently it outputs the following:

(int key = 212, string data = "U0lra36DfImFkImOkImCW4OKj4h8hIdJfoqI")
Output = {c¬a¬¬¬¬¬¬¬¬@¬¬¬¬a¬¬.c¬¬}


(int key = -88, string data = "T1RXYmV0cHFkZ3R1MzQ1Ng==")
Output = {crnobers1234}
4
  • Which output is correct and which is not? Is the one with the negative key correct? The input data for the first example doesn't look right. What was the original unencoded string? Commented Nov 26, 2010 at 3:36
  • This is a working javascript version: bypass.rd.to/decoder.php Commented Nov 26, 2010 at 3:40
  • What does the "decoder" function called in the Javascript version do? I assume it is mean to decode a Base64 string based on your C# version? Commented Nov 26, 2010 at 3:41
  • That is correct. The link i posted above shows a working example. Commented Nov 26, 2010 at 3:42

1 Answer 1

2

This code gives the same results as your example, for the two sample inputs:

    public string decoder(string data)
    {
        string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        char o1, o2, o3;
        int h1, h2, h3, h4, bits, i = 0;
        string enc = "";
        do
        {
            h1 = b64.IndexOf(data.Substring(i++, 1));
            h2 = b64.IndexOf(data.Substring(i++, 1));
            h3 = b64.IndexOf(data.Substring(i++, 1));
            h4 = b64.IndexOf(data.Substring(i++, 1));
            bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
            o1 = (char)(bits >> 16 & 0xff);
            o2 = (char)(bits >> 8 & 0xff);
            o3 = (char)(bits & 0xff);
            if (h3 == 64) enc += new string(new char[] { o1 });
            else if (h4 == 64) enc += new string(new char[] { o1, o2 });
            else enc += new string(new char[] { o1, o2, o3 });
        } while (i < data.Length);
        return enc;
    }

    public string d(int key, string data)
    {
        int i;
        string strInput = decoder(data);
        StringBuilder strOutput = new StringBuilder("");

        int intOffset = (key + 112) / 12;
        for (i = 4; i < strInput.Length; i++)
        {

            int thisCharCode = strInput[i];
            char newCharCode = (char)(thisCharCode - intOffset);
            strOutput.Append(newCharCode);
        }
        return strOutput.ToString();
    }

I'm sure it could do with some cleaning up! It's just a verbatim translation of your Javascript decoder() function that did it.

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

2 Comments

Thank you. Did you convert it yourself or did you use a program?
Converted it myself, syntax is pretty close between Javascript and C#. :-) Just a bit of fiddling with types.

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.