110,536
社区成员
发帖
与我相关
我的任务
分享
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;
#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
public byte[] ConvertHexToBytes(string _HexText)
{
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;
}