Làm phiền mọi người một chút!
Chương trình mã hóa sử dụng password như sau:
Program Code_Decode_file;
Uses Crt;
Type
file_name=string[11];
Password=string;
message=string;
coded=record
data:integer;
End;
Var
Source_file,Destination_file:file_name;
Decode:Boolean;{Code or Decode file}
Pword
assword;
(*************************)
Procedure Author;
Begin
Writeln('Code file program');
Writeln('Writen by Nguyen Anh Quyen');
Writeln('HYUNDAI-DONGANH');
Writeln('Steel tower manufacturing co,');
End;
(*************************)
Procedure error_message(mess:message);
Begin
Writeln;Write(mess);
Readln;Halt;
End;
(*************************)
Procedure Code_or_Decode(Var uncode:boolean);
Var choice:char;
Begin
Writeln(' Enter : ');
Writeln(' 1 => Code a file.');
Writeln(' 2 => Decode a coded file.');
Write (' Choose : ');
choice:=readkey;writeln(choice);
If choice='1' then uncode:=false
Else If choice='2' then uncode:=true else Halt;
End;
(*************************)
Function Upcase_filename(filename:file_name):file_name;
Var i:byte;
Begin
for i := 1 to Length(filename) do
filename
:= UpCase(filename);
Upcase_filename:=filename;
End;
(*************************)
Procedure read_file_name(var sfile,dfile:file_name);
Begin
Write('Enter Source File : ');Readln(sfile);
Write('Enter Destination File : ');Readln(dfile);
If Upcase_filename(sfile)=Upcase_filename(dfile) then
Error_Message('You can not code or decode into itself !');
End;
(*************************)
Procedure Read_password(var pwassword);
Begin
Write('Enter PassWord : ');Readln(pw);
If pw='' then pw:='NguyenAnhQuyen';
Writeln('Wait . . .');
End;
(*************************)
Procedure check_Password(pwassword;sfile:file_name);
Var f1:file of coded;
i,len_old_pw:integer;
pw_code:coded;
old_pwassword;
Begin
Assign(f1,sfile);
{$I-} (* Kiem tra su ton tai cua file *)
Reset(f1);
If IOResult<>0 then error_message('File '+sfile+' does not exist
!');
{$I+}
Read(f1,pw_code);
len_old_pw:=pw_code.data;
i:=1;old_pw:='';
While i<=len_old_pw do
Begin
read(f1,pw_code);
old_pw:=old_pw+chr(pw_code.data-ord(pw));
i:=i+1;
End;
Close(f1);
If old_pw<>pw then error_message('Invalid PassWord !');
End;
(*************************)
Procedure Code_file(pwassword;sfile,dfile:file_name);
Var f1:file of char;
f2:file of coded;
code:coded;
ch:char;
i:integer;
Begin
Assign(f1,sfile);
{$I-} (* Kiem tra su ton tai cua file *)
Reset(f1);
If IOResult<>0 then error_message('File '+sfile+' does not exist
!');
{$I+}i:=1;
Assign(f2,dfile);
Rewrite(f2);
code.data:=length(pw);
Write(f2,code);
While i<=length(pw) do
Begin
code.data:=ord(pw)+ord(pw);
Write(f2,code);
i:=i+1;
End;i:=1;
While not eof(f1) do
Begin
read(f1,ch);
code.data:=(ord(ch)+ord(pw))*3;
Write(f2,code);
i:=i+1;
If i>length(pw) then i:=1;
End;
Close(f1);Close(f2);
End;
(*************************)
Procedure UnCode_file(pwassword;sfile,dfile:file_name);
Var f1:file of coded;
f2:file of char;
code:coded;
ch:char;
i,len_old_pw:integer;
Begin
Assign(f1,sfile);
Reset(f1);
Assign(f2,dfile);
Rewrite(f2);
Read(f1,code);
len_old_pw:=code.data;
For i:=1 to len_old_pw do
read(f1,code);
i:=1;
While not eof(f1) do
Begin
read(f1,code);
ch:=chr((code.data div 3)-ord(pw));
Write(f2,ch);
i:=i+1;
If i>length(pw) then i:=1;
End;
close(f1);close(f2);
End;
(*************************)
Begin
Code_or_Decode(Decode);
read_file_name(source_file,destination_file);
Read_password(pword);
If not decode then
Code_file(pword,source_file,destination_file)
Else
Begin
check_Password(pword,source_file);
UnCode_file(pword,source_file,destination_file);
End;
Author;
End.
Tuy nhiên luc' chạy thì nó cứ báo lỗi khi chỉ ra file cần mã hóa hoặc giải mã! Thậm chí khi file mã hóa và giải mã trùng tên cũng báo lỗi!
Có ai giúp tôi đc ko?