如何对文件进行加密/解密?

cqpipilu 2005-10-08 02:13:57
想把文件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;





...全文
277 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
leonkim 2005-10-10
  • 打赏
  • 举报
回复
procedure Encrypt(Source : Pchar; Key : Pchar; Dest:Pchar);stdcall;
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;


给你个加密的例子,比较简单,加密文件可以分段读取,加密后写入。
h2yang 2005-10-10
  • 打赏
  • 举报
回复
用unit base64加密。里面可以对文件进行加密并生成新文件。如果你觉得加密强度还不够,就调用它N次。
cqpipilu 2005-10-08
  • 打赏
  • 举报
回复
那应该怎么写呢?给点例子吧
yanlls 2005-10-08
  • 打赏
  • 举报
回复
肯定不行

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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