因和别人一起做一个系统,他用的是DELPHI。
结果在用户那里做了一个加密函数
而我实现的却是用ASP.NET c# 来做
我本身也很菜,所以根本不知道该怎么去转换这个函数
const
XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47); //字符串加密用function Enc(Str: string): string; //字符加密函數 這是用的一個異或加密
var
i, j: Integer;
begin
Str := Trim(Str);
Result := '';
j := 0;
for i := 1 to Length(Str) do
begin
Result := Result + IntToHex(Byte(Str[i]) xor XorKey[j], 2);
j := (j + 1) mod 8;
end;
end;function Dec(Str: string): string; //字符解密函數
var
i, j: Integer;
begin
Result := '';
j := 0;
for i := 1 to Length(Str) div 2 do
begin
Result := Result + Char(StrToInt('$' + Copy(Str, i * 2 - 1, 2)) xor XorKey[j]);
j := (j + 1) mod 8;
end;
end;
麻烦帮忙能转成c#的函数吗?
万分感谢?
结果在用户那里做了一个加密函数
而我实现的却是用ASP.NET c# 来做
我本身也很菜,所以根本不知道该怎么去转换这个函数
const
XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47); //字符串加密用function Enc(Str: string): string; //字符加密函數 這是用的一個異或加密
var
i, j: Integer;
begin
Str := Trim(Str);
Result := '';
j := 0;
for i := 1 to Length(Str) do
begin
Result := Result + IntToHex(Byte(Str[i]) xor XorKey[j], 2);
j := (j + 1) mod 8;
end;
end;function Dec(Str: string): string; //字符解密函數
var
i, j: Integer;
begin
Result := '';
j := 0;
for i := 1 to Length(Str) div 2 do
begin
Result := Result + Char(StrToInt('$' + Copy(Str, i * 2 - 1, 2)) xor XorKey[j]);
j := (j + 1) mod 8;
end;
end;
麻烦帮忙能转成c#的函数吗?
万分感谢?
自己写个不就完了吗
要不就研究下Delphi
看懂了自己写
换的话也不行,因为他的系统也在用着。
inttohex是一个转换为16进制函数。
我水平比较差。写了一个转换函数。
结果发现得出的值和他的不一样
然后加入有中文字符。结果也完全不一样
所以求助各位达人
达人们帮忙一下。写出后立马结题
http://topic.csdn.net/u/20100510/11/5288a054-1fcb-410d-ba9d-ad8575e64180.html
{
byte[] _ImageBytes = new byte[_HexText.Length / 2]; for (int i = 0; i != _ImageBytes.Length; i++)
{
_ImageBytes[i] = Convert.ToByte(_HexText.Substring(i * 2, 2), 16);
}
return _ImageBytes;
}
public string ConvertBytesToHex(byte[] bytes)
{
string ret="";
foreach(byte bt in bytes)
{
ret+=bt.ToString("x2");
}
return ret;
}
ps:不小心发到另一个贴子去了
public string Enc(string s)
{
string str = s.Trim();
string Result = "";
int j = 0; for (int i = 1; i < str.Length; i++)
{
Result += str[i] ^ j;
j = (j + 1) % 8;
}
return Result;
} public string Dec(string s)
{
string str = s.Trim();
string Result = "";
int j = 0;
for (int i = 1; i < str.Length / 2; i++)
{
Result += Convert.ToChar('$' + (str.ToCharArray(i * 2 - 1, 2)[0] ^ j));
j = (j + 1) % 8;
}
return Result;
}
您好,将您的这个用来测试,得出的结果还是不一样。
比如。123加密后是 “833B99”
另外,如果加密的字符串是中文的话。解密出来时乱码。原程序定义了一个数组。const
XorKey: array[0..7] of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47); //字符串加密用然后IntToHex(Byte(Str[i]) xor XorKey[j], 2);
用INTTOHEX 转换后又 xor XorKey[j], 2);应该是异或运算什么。
反正这个就是我不懂的啦。
而且中文字符串好想也有问题。
麻烦您能帮忙看看吗?
您好,根据您的这个,得出的结果也是不一样的。
以下这个是我以前写的中文字符是乱码。
英文的在16进制的转换上没有问题,但是和那个DELPHI的加密结果是出入的
我个人觉得可能是在数据的匹配上。
但是我水平太菜了。
搞不清楚那个XOR异或运算
所以得出的结果老是错误。
麻烦您这边帮忙看看好吗 #region 把16进制字符串转换成普通字符串 private string HexStrToStr(string text)
{
string result = "";
byte[] bytes = new byte[text.Length / 2];
int i = 0, r = 0;
while (i < text.Length)
{
bytes[r] = byte.Parse(text.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
i += 2;
r++;
}
result = Encoding.GetEncoding("GB2312").GetString(bytes);
return result;
} #endregion #region 把字符串转换成16进制字符串 private string ChangeStrToHex(string text)
{
string result = string.Empty;
char[] chars = text.ToCharArray();
if (chars != null)
{
foreach (char c in chars)
{
int letter = Convert.ToInt32(c);
result += String.Format("{0:X}", letter);
}
}
return result;
} #endregion
123456 === 833B9961A65B
chengxu === D161CF3BF415F1
刘德华 === 73FC1F9728C7