JavaScript改写成C#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
是一个加密函数,现怎么改为C#,写成一个方法,在服务器端运行
<SCRIPT language= "JavaScript ">
function Encrypt(s)
{
var k, tmp, tmpstr;
var key_S = "43,54,75,2,687,43,65,43,4,23,34,65,43,8,33,33,54,3 ";
key = key_S.split( ", ");
tmpstr = " ";
k = 0;
for(var j = 0; j < s.length; j++){
tmp = (s.charAt(j).charCodeAt() ^ key[k].valueOf(Number));
s_hex = tmp.toString(16);
while (s_hex.length <2){
s_hex = '0 ' + s_hex;
}
tmpstr = tmpstr + s_hex;
k++;
}
return tmpstr.toUpperCase();
}
</SCRIPT>
------解决方案--------------------public string Encrypt(string s)
{
int k = 0, tmp;
string tmpstr = " ";
string key_s = "43,54,75,2,687,43,65,43,4,23,34,65,43,8,33,33,54,3 ";
string[] key = key_s.Split( ', ');
for (int j = 0; j < s.Length; j++)
{
tmp = (s[j] ^ int.Parse(key[k]));
string s_hex = tmp.ToString( "X ");
while (s_hex.Length < 2)
{
s_hex = '0 ' + s_hex;
}
tmpstr = tmpstr + s_hex;
k++;
}
return tmpstr.ToUpper();
}
------解决方案--------------------呵呵,转换字符串的时候没注意要转成16进制。呵呵
public string Encrypt(string s)
{
int k = 0, tmp;
string tmpstr = " ";
string key_s = "43,54,75,2,687,43,65,43,4,23,34,65,43,8,33,33,54,3 ";
string[] key = key_s.Split( ', ');
for (int j = 0; j < s.Length; j++)
{
tmp = (s[j] ^ int.Parse(key[k]));
tmpstr = tmpstr + tmp.ToString( "x ").PadLeft(2, '0 ');
k++;
}
return tmpstr.ToUpper();
}