请帮忙改一段delphi的代码,谢谢!
这是一段加密代码,请帮我改成C#版的,谢谢……
function myenctstr(const s:string; skey:string):string;
var
i,j: integer;
hexS,hexskey,midS,tmpstr:string;
a,b,c:byte;
begin
hexS :=myStrtoHex(s);
hexskey:=myStrtoHex(skey);
midS :=hexS;
for i:=1 to (length(hexskey) div 2) do
begin
if i <> 1 then midS:= tmpstr;
for j:=1 to (length(midS) div 2) do
begin
a:=strtoint( '$ '+midS[2*j-1]+midS[2*j]);
showmessage(inttostr(a));
b:=strtoint( '$ '+hexskey[2*i-1]+hexskey[2*i]);
c:=a xor b;
tmpstr := tmpstr+myStrtoHex(chr(c));
end;
end;
result := tmpstr;
end;
------解决方案--------------------你看这样可以 。。有错的话不好意思
public string myenctstr(string s,string skey)
{
string hexS,hexskey,midS,tmpstr;
byte a,b,c;
hexS=myStrtoHex(s);
hexskey=myStrtoHex(skey);
midS=hexS;
for(int i=1;i <length(hexskey)/2;i++)
{
if(i!=1)
{
midS= tmpstr;
}
for(int j=1;length(midS)/2;i++)
{
a=Convert.ToInt32( '$ '+midS[2*j-1]+midS[2*j]);
// showmessage(inttostr(a));
b=Convert.ToInt32( '$ '+hexskey[2*i-1]+hexskey[2*i]);
c=a^b;
tmpstr= tmpstr+myStrtoHex(chr(c));
}
}
result = tmpstr;
retuen result
}
------解决方案--------------------//比我想象的复杂点
//不太清楚TList怎么简单复制,我目前是通过循环复制
//楼主如果你简化了代码也别忘记贴出来
//这贴花了不少时间-_-!!!
public string StrToHex(List <byte> mStr) //返回处理后的十六进制字符串
{
string vReturn = " ";
foreach (char Value in mStr)
vReturn += ((byte)Value).ToString( "X2 ");
return vReturn;
}
public string myenctstr(string S, string sKey)
{
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding( "GB2312 ");
byte[] vSBytes = Encoding.Convert(utf8, gb2312, Encoding.UTF8.GetBytes(S));
byte[] vKBytes = Encoding.Convert(utf8, gb2312, Encoding.UTF8.GetBytes(sKey));
List <byte> vReturn = new List <byte> ();
List <byte> vTemp = new List <byte> ();
foreach (byte B in vSBytes)
vTemp.Add(B);