关于加密的,急!!!

No_1gogo 2002-04-16 04:10:13
问题是加密后,解密解不回来了,为什么啊?而我用在memo里面的都没有问题啊,难道是BlockRead的问题?
const
StartKey=1227; {Start default key}
MultKey=64567; {Mult default key}
AddKey=234; {Add default key}


function TForm1.Decrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string; //解密
var
I : Byte;
begin
Result := '';
for I := 1 to Length(InString) do
begin
Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
StartKey := (Byte(InString[I]) + StartKey) * MultKey + AddKey;
end;
end;

function TForm1.Encrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string; //加密
var
I : Byte;
begin
Result := '';
for I := 1 to Length(InString) do
begin
Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
StartKey := (Byte(Result[I]) + StartKey) * MultKey + AddKey;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
datafile:file;
NumRead,NumWrite:Integer;
siz:longint;
FileOk:Boolean;
buf:string[60];
begin
FileOK:=True;
assignFile(datafile,'sys.ini');
if FileExists('sys.ini')=False then
FileOk:=False
else
begin
Reset(datafile,1);
siz:=filesize(datafile);
if siz<48 then
fileOk:=False;
end;
if FileOk=False then
begin
Rewrite(datafile,1);
buf:=Encrypt('system',StartKey,MultKey,AddKey);
Edit1.Text:=buf; //设置用户名为system
blockwrite(datafile,buf,12,NumWrite); //写入用户名
buf:=Encrypt('system',StartKey,MultKey,AddKey);
Edit2.Text:=buf; //设置密码为system
blockwrite(datafile,buf,12,NumWrite); //写入密码
buf:='';
buf[0]:='^';
blockwrite(datafile,buf,24,NumWrite);
closeFile(datafile);
end;
Reset(datafile,1);
Repeat //重复读取用户名信息
BlockRead(datafile,buf,12,NumRead); //读取用户名
buf:=Decrypt(buf,StartKey,MultKey,AddKey);
Edit3.Text:=buf;
{ if NumRead>10 then
User.Items.Add(Buf); }
BlockRead(datafile,buf,12,NumRead);
buf:=Decrypt(buf,StartKey,MultKey,AddKey);
Edit4.Text:=buf;
BlockRead(datafile,buf,24,NumRead);
until(NumRead=0);
closefile(datafile);
end;
...全文
55 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
AFIHA 2002-04-16
  • 打赏
  • 举报
回复
好长哦…………
chinajavis 2002-04-16
  • 打赏
  • 举报
回复
const
StartKey=1227; {Start default key}
MultKey=64567; {Mult default key}
AddKey=234; {Add default key}


function TForm1.Decrypt(const InString: string): string;
var
StartKey1, MultKey1, AddKey1, I: Integer;
begin
StartKey1 := StartKey;
MultKey1 := MultKey;
AddKey1 := AddKey;

Result := '';
for I := 1 to Length(InString) do
begin
Result := Result + CHAR(Byte(InString[I]) xor (StartKey1 shr 8));
StartKey1 := (Byte(InString[I]) + StartKey1) * MultKey1 + AddKey1;
end;
end;

function TForm1.Encrypt(const InString: string): string;
var
StartKey1, MultKey1, AddKey1, I: Integer;
begin
StartKey1 := StartKey;
MultKey1 := MultKey;
AddKey1 := AddKey;

Result := '';
for I := 1 to Length(InString) do
begin
Result := Result + CHAR(Byte(InString[I]) xor (StartKey1 shr 8));
StartKey1 := (Byte(Result[I]) + StartKey1) * MultKey1 + AddKey1;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
datafile:file;
NumRead,NumWrite:Integer;
siz:longint;
FileOk:Boolean;
buf: string[60];
buf2: array[1..60] of char;
str: string;
begin
FileOK:=True;
assignFile(datafile,'sys.ini');
if not FileExists('sys.ini') then
FileOk:=False
else
begin
Reset(datafile,1);
siz:=filesize(datafile);
if siz<36 then
fileOk:=False;
end;
if FileOk=False then
begin
Rewrite(datafile,1);
buf:=Encrypt('system'); //设置用户名为system
Edit1.Text:=buf;
blockwrite(datafile,buf[1],6,NumWrite); //写入用户名
buf:=Encrypt('system'); //设置密码为system
Edit2.Text:=buf;
blockwrite(datafile,buf[1],6,NumWrite); //写入密码
closeFile(datafile);
end;
Reset(datafile,1);

BlockRead(datafile,buf2,6,NumRead); //读取用户名
str:=copy(buf2,1,6);
str:=Decrypt(str);
Edit3.Text:=str;

BlockRead(datafile,buf2,6,NumRead); //读取密码
str:=copy(buf2,1,6);
str:=Decrypt(str);
Edit4.Text:=str;

closefile(datafile);
end;

略做了修改,望能有所帮助

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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