大佬幫我看看以下代碼是一個解密的代碼,哪里出問題了,解密出來的十六進制好像是對的,可是轉換成字串不對,是什么問題幫忙指出并告訴我怎么修改,謝謝了
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;
請問哪里不對呢
uj5u.com熱心網友回復:
上面的代碼完全沒有縮格,這樣的代碼,你自己看著舒服嗎?uj5u.com熱心網友回復:
我也是復制的,不會弄
uj5u.com熱心網友回復:
看Decode的內部處理應該是早期AnsiString時代的代碼uj5u.com熱心網友回復:
哪錯了呢
uj5u.com熱心網友回復:
并沒有看到Encode函式,誰知道對不對呢uj5u.com熱心網友回復:
有的 我發給你
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;
uj5u.com熱心網友回復:
他的代碼是對的,沒有問題uj5u.com熱心網友回復:
是對的,只是我解密
// 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
這段封包怎么才能正常顯示字串呢
uj5u.com熱心網友回復:
Edit3.Text := Decode(HexStrToStr(Edit2.Text));如果你要十六進制顯示,可以:
Edit3.Text := StrToHexStr(Decode(HexStrToStr(Edit2.Text)));
uj5u.com熱心網友回復:
大佬是這樣的
Edit3.Text := Decode(HexStrToStr(Edit2.Text));
Edit3.Text 內容 #2榔?
Edit3.Text := StrToHexStr(Decode(HexStrToStr(Edit2.Text)));
這個十六進制就好像是對的,但是把他轉成字串又是#2榔?了
uj5u.com熱心網友回復:
兩個是同樣的內容,只是第二個做了十六進制編碼而已,第一個包含不可顯示字符uj5u.com熱心網友回復:
那第一個怎么處理才能顯示出來 不可顯示的忽略掉
uj5u.com熱心網友回復:
加上% 然后url解碼即可轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/11208.html
標籤:語言基礎/算法/系統設計
上一篇:快速實作安卓狀態欄沉浸模式
