[英]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.