下面的代码是我写的一个小的加密程序,你参考一下可以。
我操作的是无类型文件,如果你做积分榜可以用其他的方法。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure bufcopy(var buf:array of byte;s:ansistring);
procedure BufXor(var buf1,buf2,buf3:array of byte);
procedure jiami(var buf1,buf2,buf3:array of byte);
function comparebuf(const buf1,buf2:array of byte):boolean;
end;
const
fixedpassword=#133#68#183#02#08#120#76#166#212#66;
fileheader=#0#0#0#0#0#0#0#0#0#0#76#10#30#100#200#81#255#0#100#200;
var
Form1: TForm1;
chgpassword,xorpassword,userpassword:array[1..20] of byte;
implementation{$R *.DFM}
//密码的最大长度是20位
{
加密的方式:
密码种类:userpassword 程序密码chgpassword 固定密码 fixedpass 异或密码 xorpassword加密的文件包括:
1.文件头 fileheader
2.用户密码长度 一个byte保存
3.程序密码 chgpassword
4.异或密码 xorpassword
5.加密文件部分
6.100字节的垃圾信息
}
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
end;
procedure TForm1.bufcopy(var buf:array of byte;s:ansistring);
var i,j,k,len:integer;
Begin
len:=length(s);
j:=low(buf);
k:=high(buf);
for i:=j to k do
Begin
if (i-j>len) or (len=0) then
begin
buf[i]:=0;
continue;
end;
buf[i]:=byte(s[i-j+1]);
End;
End;
procedure TForm1.BufXor(var buf1,buf2,buf3:array of byte);
var i,j,k:integer;
Begin
j:=low(buf1);
k:=high(buf1);
for i:=j to k do
Begin
buf3[i]:=buf1[i] xor buf2[i];
End;
End;
procedure TForm1.Button2Click(Sender: TObject);
var i,j,retn:integer; mysource,mydest:file;
bufsrc,bufdst:array[1..20] of byte;
begin
if OpenDialog1.FileName='' then
exit;
if not SaveDialog1.Execute then
exit;
Randomize;
bufcopy(userpassword,edit1.text); //用户密码产生
j:=length(edit1.text);
for i:=1 to 20 do
begin
chgpassword[i]:=Random(255);
xorpassword[i]:=chgpassword[i] ;
edit1.text;
sleep(12);
end; //机器密码产生
bufxor(chgpassword,userpassword,xorpassword); //异或密码产生
assignfile(mysource,opendialog1.filename);
try
assignfile(mydest,savedialog1.filename);
try
reset(mysource,1);
rewrite(mydest,1);
bufcopy(bufdst,fileheader);
blockwrite(mydest,bufdst,20);
bufdst[1]:=length(edit1.text);
blockwrite(mydest,bufdst,1);
blockwrite(mydest,chgpassword,sizeof(chgpassword));
blockwrite(mydest,xorpassword,sizeof(xorpassword));
repeat
blockread(mysource,bufsrc,sizeof(bufsrc),retn);
bufxor(bufsrc,xorpassword,bufdst);
blockwrite(mydest,bufdst,retn);
until retn=0;
finally
closefile(mydest);
end;
finally
closefile(mysource);
end;end;
function TForm1.comparebuf(const buf1,buf2:array of byte):boolean;
var i,mylow,myhigh:integer;
Begin
mylow:=low(buf1);
myhigh:=high(buf1);
result:=true;
for i:=mylow to myhigh do
Begin
if buf1[i]<>buf2[i] then
result:=false
End;
End;
procedure TForm1.Button3Click(Sender: TObject);
var i,retn:integer; mysource,mydest:file; bufsrc,bufdst:array[1..20] of byte;
begin
if OpenDialog1.FileName='' then
exit;
if not SaveDialog1.Execute then
exit;
assignfile(mysource,OpenDialog1.FileName);
try
reset(mysource,1);
blockread(mysource,bufsrc,sizeof(bufsrc),retn);
bufcopy(userpassword,fileheader);
if not comparebuf(bufsrc,userpassword) then
Begin
showmessage('加密文件错误!'+#13+'请确定该文件是否用本软件加密.');
exit;
End;
blockread(mysource,bufsrc,1,retn);
if length(edit1.text)<>bufsrc[1] then
Begin
showmessage('密码错误!'+#13+'您不能解密该文件');
exit;
End;
blockread(mysource,chgpassword,20,retn);
if retn<>20 then
Begin
showmessage('打开文件错误!');
exit;
End;
blockread(mysource,xorpassword,20,retn);
if retn<>20 then
Begin
showmessage('打开文件错误!');
exit;
End;
bufxor(xorpassword,chgpassword,userpassword);
bufcopy(bufdst,edit1.text);
if not comparebuf(userpassword,bufdst) then
Begin
showmessage('密码错误!'+#13+'您不能解密该文件');
exit;
End;
assignfile(mydest,savedialog1.filename);
try
rewrite(mydest,1);
repeat
blockread(mysource,bufsrc,20,retn);
bufxor(bufsrc,xorpassword,bufdst);
blockwrite(mydest,bufdst,retn);
until retn=0;
finally
closefile(mydest);
end;
finally
try
closefile(mysource);
except
end;
end;
end;end.
我操作的是无类型文件,如果你做积分榜可以用其他的方法。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure bufcopy(var buf:array of byte;s:ansistring);
procedure BufXor(var buf1,buf2,buf3:array of byte);
procedure jiami(var buf1,buf2,buf3:array of byte);
function comparebuf(const buf1,buf2:array of byte):boolean;
end;
const
fixedpassword=#133#68#183#02#08#120#76#166#212#66;
fileheader=#0#0#0#0#0#0#0#0#0#0#76#10#30#100#200#81#255#0#100#200;
var
Form1: TForm1;
chgpassword,xorpassword,userpassword:array[1..20] of byte;
implementation{$R *.DFM}
//密码的最大长度是20位
{
加密的方式:
密码种类:userpassword 程序密码chgpassword 固定密码 fixedpass 异或密码 xorpassword加密的文件包括:
1.文件头 fileheader
2.用户密码长度 一个byte保存
3.程序密码 chgpassword
4.异或密码 xorpassword
5.加密文件部分
6.100字节的垃圾信息
}
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
end;
procedure TForm1.bufcopy(var buf:array of byte;s:ansistring);
var i,j,k,len:integer;
Begin
len:=length(s);
j:=low(buf);
k:=high(buf);
for i:=j to k do
Begin
if (i-j>len) or (len=0) then
begin
buf[i]:=0;
continue;
end;
buf[i]:=byte(s[i-j+1]);
End;
End;
procedure TForm1.BufXor(var buf1,buf2,buf3:array of byte);
var i,j,k:integer;
Begin
j:=low(buf1);
k:=high(buf1);
for i:=j to k do
Begin
buf3[i]:=buf1[i] xor buf2[i];
End;
End;
procedure TForm1.Button2Click(Sender: TObject);
var i,j,retn:integer; mysource,mydest:file;
bufsrc,bufdst:array[1..20] of byte;
begin
if OpenDialog1.FileName='' then
exit;
if not SaveDialog1.Execute then
exit;
Randomize;
bufcopy(userpassword,edit1.text); //用户密码产生
j:=length(edit1.text);
for i:=1 to 20 do
begin
chgpassword[i]:=Random(255);
xorpassword[i]:=chgpassword[i] ;
edit1.text;
sleep(12);
end; //机器密码产生
bufxor(chgpassword,userpassword,xorpassword); //异或密码产生
assignfile(mysource,opendialog1.filename);
try
assignfile(mydest,savedialog1.filename);
try
reset(mysource,1);
rewrite(mydest,1);
bufcopy(bufdst,fileheader);
blockwrite(mydest,bufdst,20);
bufdst[1]:=length(edit1.text);
blockwrite(mydest,bufdst,1);
blockwrite(mydest,chgpassword,sizeof(chgpassword));
blockwrite(mydest,xorpassword,sizeof(xorpassword));
repeat
blockread(mysource,bufsrc,sizeof(bufsrc),retn);
bufxor(bufsrc,xorpassword,bufdst);
blockwrite(mydest,bufdst,retn);
until retn=0;
finally
closefile(mydest);
end;
finally
closefile(mysource);
end;end;
function TForm1.comparebuf(const buf1,buf2:array of byte):boolean;
var i,mylow,myhigh:integer;
Begin
mylow:=low(buf1);
myhigh:=high(buf1);
result:=true;
for i:=mylow to myhigh do
Begin
if buf1[i]<>buf2[i] then
result:=false
End;
End;
procedure TForm1.Button3Click(Sender: TObject);
var i,retn:integer; mysource,mydest:file; bufsrc,bufdst:array[1..20] of byte;
begin
if OpenDialog1.FileName='' then
exit;
if not SaveDialog1.Execute then
exit;
assignfile(mysource,OpenDialog1.FileName);
try
reset(mysource,1);
blockread(mysource,bufsrc,sizeof(bufsrc),retn);
bufcopy(userpassword,fileheader);
if not comparebuf(bufsrc,userpassword) then
Begin
showmessage('加密文件错误!'+#13+'请确定该文件是否用本软件加密.');
exit;
End;
blockread(mysource,bufsrc,1,retn);
if length(edit1.text)<>bufsrc[1] then
Begin
showmessage('密码错误!'+#13+'您不能解密该文件');
exit;
End;
blockread(mysource,chgpassword,20,retn);
if retn<>20 then
Begin
showmessage('打开文件错误!');
exit;
End;
blockread(mysource,xorpassword,20,retn);
if retn<>20 then
Begin
showmessage('打开文件错误!');
exit;
End;
bufxor(xorpassword,chgpassword,userpassword);
bufcopy(bufdst,edit1.text);
if not comparebuf(userpassword,bufdst) then
Begin
showmessage('密码错误!'+#13+'您不能解密该文件');
exit;
End;
assignfile(mydest,savedialog1.filename);
try
rewrite(mydest,1);
repeat
blockread(mysource,bufsrc,20,retn);
bufxor(bufsrc,xorpassword,bufdst);
blockwrite(mydest,bufdst,retn);
until retn=0;
finally
closefile(mydest);
end;
finally
try
closefile(mysource);
except
end;
end;
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货