求助:帮我看看Delphi一个解密代码哪里错了?

我还是很纯洁的 2020-04-08 07:56:27
大佬帮我看看以下代码是一个解密的代码,哪里出问题了,解密出来的十六进制好像是对的,可是转换成字符串不对,是什么问题帮忙指出并告诉我怎么修改,谢谢了
function Decode(source : string):string; //解密
function strToHexStr(str:string):string; //字符串转换成16字符串
function HexStrToStr(const S:string):string; //16进制字符串转换成字符串

//解密
function Decode(source : string):string;
var
Source_Len,Len : integer;
Count,c1,c2 : integer;
code : array[0..7] of byte;
a1,a2 : byte;
ind : dword;
Decode_Str : string;
label L1,L2;
begin
Result := '';
Decode_Str := '';
code[2] := $fc;
code[4] := $f0;
code[6] := $c0;
Len := 0;
a1 := 0;
a2 := 0;
c1 := 2;
c2 := 0;
ind := 0;
Count := 0;
Source_Len := Length(source);
while (Count < Source_Len) do
begin
if(ord(Source[Count+1]) - $3c) < 0 then
begin
Decode_Str := Decode_Str + Source[Count+1];
inc(Len);
inc(Count);
a1 := 0;
a2 := 0;
c1 := 2;
c2 := 0;
ind := 0;
Continue;
//break;
end;
a1 := ord(Source[Count+1]) - $3c;
if Len >= Source_Len then
begin
break;
end;
if (c2 + 6) < 8 then
begin
goto L2;
end;
ind := a1 and $3f;
ind := ind shr (6-c1);
Decode_Str := Decode_Str + chr(ind or a2);
Inc(Len);
c2 := 0;
if c1 >= 6 then
begin
c1 := 2;
goto L1;
end;
inc(c1,2);
L2 :a2 := a1 shl c1;
a2 := a2 and code[c1];
c2 := c2 + (8 - c1);
L1 :inc(count);
end;
SetLength(Decode_Str,Len);
Result := Decode_Str;
end;

//字符串转换成16字符串

function strToHexStr(str:string):string;
var
c:char;
ss:string;
i:integer;
begin
while str<>'' do begin
c:=str[1];
ss:=ss+format('%0x',[ord(c)]);
delete(str,1,1);
end;
strtohexStr:= ss;
end;

//16进制字符串转换成字符串
function HexStrToStr(const S:string):string;
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;

调用

procedure TForm1.Button2Click(Sender: TObject);
begin
// edit2=23 32 6C 48 56 6F 41 4C 3C 3C 3C 3C 3E 78 3C 3C 3C 3C 52 68 70 3E 3C 3C 64 5A 7A 5D 77 78 4E 63 65 48 76 79 75 46 43 6B 64 5A 78 21
edit1.Text:=strToHexStr(Decode(HexStrToStr(edit2.Text)));
edit3.Text:=HexStrToStr(edit1.Text);
end;

请问哪里不对呢
...全文
552 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Y.A.K.E 2020-06-09
  • 打赏
  • 举报
回复
加上% 然后url解码即可
  • 打赏
  • 举报
回复
两个是同样的内容,只是第二个做了十六进制编码而已,第一个包含不可显示字符

  • 打赏
  • 举报
回复
引用 10 楼 早打大打打核战争 的回复:
Edit3.Text := Decode(HexStrToStr(Edit2.Text)); 如果你要十六进制显示,可以: Edit3.Text := StrToHexStr(Decode(HexStrToStr(Edit2.Text)));
大佬是这样的 Edit3.Text := Decode(HexStrToStr(Edit2.Text)); Edit3.Text 内容 #2榔? Edit3.Text := StrToHexStr(Decode(HexStrToStr(Edit2.Text))); 这个十六进制就好像是对的,但是把他转成字符串又是#2榔?了
  • 打赏
  • 举报
回复
Edit3.Text := Decode(HexStrToStr(Edit2.Text));
如果你要十六进制显示,可以:
Edit3.Text := StrToHexStr(Decode(HexStrToStr(Edit2.Text)));
  • 打赏
  • 举报
回复
引用 8 楼 早打大打打核战争 的回复:
他的代码是对的,没有问题
是对的,只是我解密 // edit2=23 32 6C 48 56 6F 41 4C 3C 3C 3C 3C 3E 78 3C 3C 3C 3C 52 68 70 3E 3C 3C 64 5A 7A 5D 77 78 4E 63 65 48 76 79 75 46 43 6B 64 5A 78 21 这段封包怎么才能正常显示字符串呢
  • 打赏
  • 举报
回复
他的代码是对的,没有问题
  • 打赏
  • 举报
回复
引用 6 楼 早打大打打核战争 的回复:
并没有看到Encode函数,谁知道对不对呢
有的 我发给你 function Encode(source : string):string; var Source_Len,Len : integer; Count,c : integer; a1,a2 : byte; ind : dword; Encode_Str : string; begin Result := ''; Encode_Str := ''; Len := 0; a1 := 0; a2 := 0; c := 0; ind := 0; Count := 0; Source_Len := Length(source); while Count < Source_Len do begin if Len >= $2710 then break; ind := ord(source[Count+1]); ind := ind shr (c+2); a1 := ind or a2; a1 := a1 and $3f; ind := ord(source[Count+1]); ind := ind shl (8-(c+2)); ind := ind shr 2; a2 := ind and $3f; inc(c,2); if c >= 6 then begin if Len >= $270f then begin Encode_Str := Encode_Str + chr(a1 + $3c); inc(Len); end else begin Encode_Str := Encode_Str + chr(a1 + $3c); Encode_Str := Encode_Str + chr(a2 + $3c); Inc(Len,2); end; c := 0; a2 := 0; end else begin Encode_Str := Encode_Str + chr(a1 + $3c); Inc(Len); end; inc(Count); end; if c > 0 then begin Encode_Str := Encode_Str + chr(a2 + $3c); Inc(Len); end; SetLength(Encode_Str,Len); Result := Encode_Str; end;
  • 打赏
  • 举报
回复
引用 12 楼 早打大打打核战争 的回复:
两个是同样的内容,只是第二个做了十六进制编码而已,第一个包含不可显示字符
那第一个怎么处理才能显示出来 不可显示的忽略掉
  • 打赏
  • 举报
回复
并没有看到Encode函数,谁知道对不对呢
  • 打赏
  • 举报
回复
引用 4 楼 早打大打打核战争 的回复:
看Decode的内部处理应该是早期AnsiString时代的代码
哪错了呢
  • 打赏
  • 举报
回复
看Decode的内部处理应该是早期AnsiString时代的代码
  • 打赏
  • 举报
回复
引用 2 楼 BlueStorm 的回复:
上面的代码完全没有缩格,这样的代码,你自己看着舒服吗?
我也是复制的,不会弄
BlueStorm 2020-04-08
  • 打赏
  • 举报
回复
上面的代码完全没有缩格,这样的代码,你自己看着舒服吗?

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧