-
-
[作品提交]代码变异加密工具
-
发表于:
2008-6-17 17:41
13631
-
思路:将程序中关键代码强行变异,让它执行不下去,出错。。。
逆向调试也将无法进行,以达到保护作用。
加密工具代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, XPMan;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Edit3: TEdit;
Button2: TButton;
OpenDialog: TOpenDialog;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button3: TButton;
XPManifest: TXPManifest;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function hextoint(s: string): Integer;
begin //$代表16进制
Result:=StrToInt('$'+s);
end;
function Stream_SearchString( // 在流中搜索字符串
mStream: TStream; // 目标流
mStr: string; // 字符串
mStartAddress: Integer = 0; // 起始地址
mEndAddress: Integer = MaxInt; // 终止地址
mIgnoreCase: Boolean = False // 是否忽略大小写
): Integer; // 返回字符串所在的位置
const
cBufferSize = $1000;
var
S: string;
T: string;
I: Integer;
J: Integer;
L: Integer;
begin
Result := -1;
if not Assigned(mStream) then Exit;
if mStr = '' then Exit;
L := Length(mStr);
mStream.Position := mStartAddress;
if mIgnoreCase then mStr := UpperCase(mStr);
T := '';
J := mStartAddress;
while mStream.Position <= mEndAddress do
begin
SetLength(S, cBufferSize);
I := mStream.Read(S[1], cBufferSize);
SetLength(S, I);
if S = '' then Exit;
if mIgnoreCase then S := UpperCase(S);
Result := Pos(mStr, T + S) - 1;
if Result >= 0 then
begin
Result := Result + J - Length(T);
Exit;
end;
T := Copy(S, cBufferSize - L, MaxInt);
Inc(J, I);
end;
end; { Stream_SearchString }
function File_SearchString( // 在文件中搜索字符串
mFileName: string; // 文件名
mStr: string; // 字符串
mStartAddress: Integer = 0; // 起始地址
mEndAddress: Integer = MaxInt; // 终止地址
mIgnoreCase: Boolean =true // 是否忽略大小写
): Integer; // 返回字符串所在的位置
var
vFileStream: TFileStream;
vFileHandle: THandle;
begin
Result := -1;
if not FileExists(mFileName) then Exit;
vFileHandle := _lopen(PChar(mFileName), OF_READ or OF_SHARE_DENY_NONE); //06-09-25 No.1 ZswangY37
if Integer(vFileHandle) <= 0 then Exit;
vFileStream := TFileStream.Create(vFileHandle);
try
Result := Stream_SearchString(vFileStream, mStr,
mStartAddress, mEndAddress, mIgnoreCase);
finally
vFileStream.Free;
end;
end; { File_SearchString }
Procedure XorData(vFileName:string; vStart, vEnd: Integer); //===数据加密
var
vMemoryStream: TMemoryStream;
vBuffer: string;
I: Integer;
begin
if not FileExists(vFileName) then
begin
application.MessageBox('指定的来原文件不存在!','提示',MB_ok+MB_Iconinformation);
Exit;
end;
SetLength(vBuffer, vEnd - vStart + 1);
vMemoryStream := TMemoryStream.Create;
try
vMemoryStream.LoadFromFile(vFileName);
vMemoryStream.Seek(vStart, soFromBeginning);
vMemoryStream.Read(vBuffer[1], Length(vBuffer));
for I := 1 to Length(vBuffer) do
vBuffer[I] := Chr(Ord(vBuffer[I]) xor $51DE003A);
vMemoryStream.Seek(vStart, soFromBeginning);
vMemoryStream.Write(vBuffer[1], Length(vBuffer));
vMemoryStream.SaveToFile(vFileName);
finally
vMemoryStream.Free;
showmessage('处理成功!');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog.Execute then
Edit1.Text:=OpenDialog.FileName;
end;
procedure TForm1.Button3Click(Sender: TObject); //自动定位,地址可能有所偏差
begin
Edit2.Text:= IntToHex(File_SearchString(Edit1.text,'xor_begin')-212,8);
Edit3.Text:= IntToHex(File_SearchString(Edit1.text,'xor_end')-117,8);
end;
procedure TForm1.Button2Click(Sender: TObject);
var i1,i2:integer;
begin
i1:=hextoint(Edit2.Text);
i2:=hextoint(Edit3.Text);
XorData(Edit1.Text,i1,i2); //Xor
end;
end.
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课