想把文件C:\123.txt进行加密
基本思想是把文件读入流,然后按位或字节处理后,写入文件,实现加密.
不知道这样做是否可行,执行会出错,求指点var str : TFileStream;
enc : TFileStream;
i : integer;
s : string;
buf : byte; s:='c:\123.txt';
str:=TFileStream.create(s,fmopenread);
enc:=TFileStream.create('c:\123_enc.txt',fmcreate);
for i:=1 to str.size do
begin
str.readbuffer(buf,8);
buf:=buf xor 8;
enc.write(buf,8);
end;
基本思想是把文件读入流,然后按位或字节处理后,写入文件,实现加密.
不知道这样做是否可行,执行会出错,求指点var str : TFileStream;
enc : TFileStream;
i : integer;
s : string;
buf : byte; s:='c:\123.txt';
str:=TFileStream.create(s,fmopenread);
enc:=TFileStream.create('c:\123_enc.txt',fmcreate);
for i:=1 to str.size do
begin
str.readbuffer(buf,8);
buf:=buf xor 8;
enc.write(buf,8);
end;
var
Ch:pchar;
KeyStart:pchar;
DestStr : string;
begin
ch := dest;
if Key^ = #0 then
begin
Key := '&%~*03jfjs9^%8@09ufhanvmb&*%oqry';
end; KeyStart := Key; while Source^ <> #0 do
begin
ch^ := char((ord(Source^)and $f0) + (byte(Source^) and $0f) xor (ord(key^)and $0f));
inc(ch);
inc(Source);
inc(key);
if key^ = #0 then
Key := KeyStart;
end;
ch^ := #0;
end;procedure Decrypt(Source : Pchar; Key : Pchar; Dest:Pchar);stdcall;
var
Ch:pchar;
KeyStart:pchar;
DestStr : string;
begin
base64.DecodeData(Source,DestStr);
Source := Pchar(DestStr); ch := dest;
if Key^ = #0 then
begin
Key := '&%~*03jfjs9^%8@09ufhanvmb&*%oqry';
end; KeyStart := Key; while Source^ <> #0 do
begin
ch^ := char((ord(Source^)and $f0) + (byte(Source^) and $0f) xor (ord(key^)and $0f));
inc(ch);
inc(Source);
inc(key);
if key^ = #0 then
Key := KeyStart;
end;
ch^ := #0;
end;
给你个加密的例子,比较简单,加密文件可以分段读取,加密后写入。