unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
key: byte;
implementation
uses shellapi, ShlObj;
{$R *.dfm}
procedure GenerateSimleKey;
var
tmp_str: array[0..25] of char;
serial: DWORD;
lkey: Byte;
begin
SHGetSpecialFolderPath(0, @tmp_str[0], CSIDL_DESKTOP, false);
tmp_str[3] := #0;
asm
push 0
push 0
push 0
push 0
lea edi, serial
push edi
push 0
push 0
lea edi, tmp_str
push edi
call GetVolumeInformation
mov eax, serial
mov ecx, 4
XOR bl, bl
@@loop:
XOR bl, al
SHR eax, 8
loop @@loop
mov lkey, bl
end;
key := lkey;
end;
function HexToInt(Value: string): Integer;
var
I: Integer;
begin
Result := 0;
I := 1;
if Value = '' then
Exit;
if Value[1] = '$' then
Inc(I);
while I <= Length(Value) do
begin
if Value[I] in ['0'..'9'] then
Result := (Result shl 4) or (Ord(Value[I]) - Ord('0'))
else if Value[I] in ['A'..'F'] then
Result := (Result shl 4) or (Ord(Value[I]) - Ord('A') + 10)
else if Value[I] in ['a'..'f'] then
Result := (Result shl 4) or (Ord(Value[I]) - Ord('a') + 10)
else
Break;
Inc(I);
end;
end;
function decodeString(str: string): string;
var
i, len: Integer;
begin
Result := '';
len := length(str);
for i := 1 to len do
if i mod 2 <> 0 then
Result := Result + Chr(HexToInt(Copy(str, i, 2)) xor key);
end;
function encodeString(str: string): string;
var
i, len: Integer;
begin
Result := '';
len := length(str);
for i := 1 to len do
Result := Result + IntToHex(Ord(str[i]) xor key, 2);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GenerateSimleKey;
Memo1.Text:=decodeString(encodeString('abcabc'));//; //结果abcabc
end;
end.