簡體   English   中英

將JavaScript轉換為C#

[英]Converting JavaScript to C#

在將這個javascript轉換為C#時,我有點困惑...

任何幫助,將不勝感激!

這是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)
}

這是我嘗試將其轉換為C#。 它在某些時候有效,但大多數情況下負數作為關鍵......

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

目前它輸出以下內容:

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


(int key = -88, string data = "T1RXYmV0cHFkZ3R1MzQ1Ng==")
Output = {crnobers1234}

對於兩個示例輸入,此代碼給出的結果與您的示例相同:

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

我敢肯定它可以做一些清理! 只是做到這一點的Java解碼器()函數的逐字翻譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM