2010-11-26 82 views
0

将此JavaScript转换为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} 
+0

哪个输出正确,哪个不正确?带有否定键的人是否正确?第一个例子的输入数据看起来不正确。什么是原始的未编码字符串? – 2010-11-26 03:36:46

+0

这是一个可用的javascript版本:http://bypass.rd.to/decoder.php – E3pO 2010-11-26 03:40:49

回答

2

这个代码给出了相同的结果。你的榜样,对于两个样本输入:

public string decoder(string data) 
    { 
     string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 
     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(); 
    } 

我确信它可以做一些清理!这只是你的Javascript解码器()函数的逐字翻译。