结贴,近来领分 ( 积分: 70 )

  • 主题发起人 主题发起人 时报平
  • 开始时间 开始时间

时报平

Unregistered / Unconfirmed
GUEST, unregistred user!
因为是简单的xor,所以加密与解密是同一个算法。
但是有时会出现丢数据的现象,'6'这个数据有时会丢。
不知是我这里的问题,还是别的问题。
function TForm_pwdfile.setpwdfile(soure_filename:string;dest_filename:string;flag:integer):string;
var
soure_file,dest_file:file;
buffer: byte;
bytesread,totalread,byteswritten,fsize:integer;
soure_str:string ;
pwd_str:string;
pwd:byte;
begin

assignfile(soure_file,soure_filename);
reset(soure_file,1);
assignfile(dest_file,dest_filename);
try
rewrite(dest_file,1);
try
try
if flag=0 then

begin

pwd:=RandomRange(10,254) ;//------??随机数,解密时不会出错吗? ,这里改为RandomRange(32,254) ,就好了
buffer:=pwd;
pwd_str:=char(pwd );
blockwrite(dest_file,buffer,1,byteswritten);
end
else

begin

blockread(soure_file,buffer,sizeof(buffer),bytesread);
pwd:=buffer;
end;

totalread:=0;
fsize:=filesize(soure_file);
repeat
blockread(soure_file,buffer,sizeof(buffer),bytesread);
if bytesread>0 then

begin

buffer:=setpasswrod(buffer ,pwd );
blockwrite(dest_file,buffer,bytesread,byteswritten);
if bytesread<>byteswritten then

raise exception.Create('复制文件出错!')
else

begin

totalread:=totalread+bytesread;
end;

end
until bytesread=0;
except
erase(dest_file);
raise;
end;

finally
closefile(dest_file);
closefile(soure_file);
end;

finally
end;

function TForm_pwdfile.setpasswrod(sou_str:variant;pwd_str:variant):variant;
var
result_str:variant;
soure_str,pswd_str:variant;
begin

soure_str:=sou_str;
pswd_str:=pwd_str;
result_str:=soure_str xor pswd_str;
result:=result_str;
end;


附:同事的用C语言进行的对应加密/解密算法。
经我的算法加密后的文件,在他那里发现问题。 到底是我的问题,还是他的问题呢?
int
my_encrypt(file1,file2)
char *file1;
char *file2;
{
FILE *fp,*fp2;
char ch,ch1;

if((fp=fopen(file1,&quot;r&quot;)) == NULL){
return (-1);
}
if((fp2=fopen(file2,&quot;w&quot;)) == NULL){
return (-1);
}
ch = fgetc(fp);
ch1 = fgetc(fp);
while (!feof(fp)) {
ch1 = ch1 ^ ch;
fputc(ch1,fp2);
ch1 = fgetc(fp);
}
fclose(fp);
fclose(fp2);
return 0;
}
int
my_crypt(file1,file2,ch)
char *file1;
char *file2;
char ch;
{
FILE *fp,*fp2;
char ch1;
int i;

if((fp=fopen(file1,&quot;r&quot;)) == NULL){
return (-1);
}
if((fp2=fopen(file2,&quot;w&quot;)) == NULL){
return (-1);
}
fputc(ch,fp2);
ch1 = fgetc(fp);
while (!feof(fp)) {
ch1 = ch1 ^ ch;
fputc(ch1,fp2);
ch1 = fgetc(fp);
}
fclose(fp);
fclose(fp2);
return 0;
}
发分。。
 
因为是简单的xor,所以加密与解密是同一个算法。
但是有时会出现丢数据的现象,'6'这个数据有时会丢。
不知是我这里的问题,还是别的问题。
function TForm_pwdfile.setpwdfile(soure_filename:string;dest_filename:string;flag:integer):string;
var
soure_file,dest_file:file;
buffer: byte;
bytesread,totalread,byteswritten,fsize:integer;
soure_str:string ;
pwd_str:string;
pwd:byte;
begin

assignfile(soure_file,soure_filename);
reset(soure_file,1);
assignfile(dest_file,dest_filename);
try
rewrite(dest_file,1);
try
try
if flag=0 then

begin

pwd:=RandomRange(10,254) ;//------??随机数,解密时不会出错吗? ,这里改为RandomRange(32,254) ,就好了
buffer:=pwd;
pwd_str:=char(pwd );
blockwrite(dest_file,buffer,1,byteswritten);
end
else

begin

blockread(soure_file,buffer,sizeof(buffer),bytesread);
pwd:=buffer;
end;

totalread:=0;
fsize:=filesize(soure_file);
repeat
blockread(soure_file,buffer,sizeof(buffer),bytesread);
if bytesread>0 then

begin

buffer:=setpasswrod(buffer ,pwd );
blockwrite(dest_file,buffer,bytesread,byteswritten);
if bytesread<>byteswritten then

raise exception.Create('复制文件出错!')
else

begin

totalread:=totalread+bytesread;
end;

end
until bytesread=0;
except
erase(dest_file);
raise;
end;

finally
closefile(dest_file);
closefile(soure_file);
end;

finally
end;

function TForm_pwdfile.setpasswrod(sou_str:variant;pwd_str:variant):variant;
var
result_str:variant;
soure_str,pswd_str:variant;
begin

soure_str:=sou_str;
pswd_str:=pwd_str;
result_str:=soure_str xor pswd_str;
result:=result_str;
end;


附:同事的用C语言进行的对应加密/解密算法。
经我的算法加密后的文件,在他那里发现问题。 到底是我的问题,还是他的问题呢?
int
my_encrypt(file1,file2)
char *file1;
char *file2;
{
FILE *fp,*fp2;
char ch,ch1;

if((fp=fopen(file1,&quot;r&quot;)) == NULL){
return (-1);
}
if((fp2=fopen(file2,&quot;w&quot;)) == NULL){
return (-1);
}
ch = fgetc(fp);
ch1 = fgetc(fp);
while (!feof(fp)) {
ch1 = ch1 ^ ch;
fputc(ch1,fp2);
ch1 = fgetc(fp);
}
fclose(fp);
fclose(fp2);
return 0;
}
int
my_crypt(file1,file2,ch)
char *file1;
char *file2;
char ch;
{
FILE *fp,*fp2;
char ch1;
int i;

if((fp=fopen(file1,&quot;r&quot;)) == NULL){
return (-1);
}
if((fp2=fopen(file2,&quot;w&quot;)) == NULL){
return (-1);
}
fputc(ch,fp2);
ch1 = fgetc(fp);
while (!feof(fp)) {
ch1 = ch1 ^ ch;
fputc(ch1,fp2);
ch1 = fgetc(fp);
}
fclose(fp);
fclose(fp2);
return 0;
}
发分。。
 
顶一下!
 
接受答案了.
 
后退
顶部