• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi文件/流的加密解密方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
http://www.inbytes.net/
*
* 作  者: [email protected]
*
* 使用说明: 此文件的代码可以直接编译使用,
*      您可以根据需要编写自己的加密/解密代码.
*
* 声  明: 以下代码属作者原创, 请转载时保留本信息.      
*
*)

unit uCompress;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, ComCtrls, Dialogs;

// 压缩/解压文件
procedure CompressFile(Source, Target: String); stdcall;
procedure DecompressFile(Source, Target: String); stdcall;

// 压缩/解压文件到流
procedure CompressToStream(FileName: String; Stream: TStream); stdcall;
procedure DecompressToStream(FileName: String; Stream: TStream); stdcall;

// 压缩/解压流
procedure CompressStream(InStream, OutStream: TStream); stdcall;
procedure DecompressStream(InStream, OutStream: TStream); stdcall;

implementation

uses
ZLib;

const
COMPRESS_ERROR = '压缩文件时出现内部错误:';
DECOMPRESS_ERROR = '解压文件时出现内部错误:';
COMPRESS_STRM_ERROR = '压缩流时出现内部错误:';
DECOMPRESS_STRM_ERROR = '解压流时出现内部错误:';
BufSize = $4096;

// 压缩文件
procedure CompressFile(Source, Target: String);
var
i: Integer;
Buf: array[0..BufSize] of byte;
ComStream: TCompressionStream;
InStream, OutStream: TFileStream;
begin
if not FileExists(Source) then
Exit;
InStream := Nil;
OutStream := nil;
ComStream := nil;
try
// 生成流
InStream := TFileStream.Create(Source, fmOpenRead OR fmShareDenyNone);
OutStream := TFileStream.Create(Target, fmCreate OR fmShareDenyWrite);
ComStream := TCompressionStream.Create(clMax, OutStream);

// 压缩流
for i := 1 to (InStream.Size div BufSize) do begin
InStream.ReadBuffer(Buf, BufSize);
ComStream.Write(Buf, BufSize);
end;

i := InStream.Size mod BufSize;
if (i > 0) then begin
InStream.ReadBuffer(Buf, i);
ComStream.Write(Buf, i);
End;

InStream.Free;
InStream := nil;

// 注先后
ComStream.Free;
ComStream := nil;

// 在此写加密流代码(要先释放 ComStream)
// EncryptStream(OutStream);

OutStream.Free;
OutStream := nil;
except
on E: Exception do begin
if (InStream <> nil) then
InStream.Free;
if (OutStream <> nil) then
OutStream.Free;
if (ComStream <> nil) then
ComStream.Free;
MessageDlg(COMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 解压文件
procedure DecompressFile(Source, Target: String);
var
i: Integer;
Buf: array[0..BufSize] of Byte;
DecomStream: TDecompressionStream;
MemStream: TMemoryStream;
OutStream: TFileStream;
begin
if not FileExists(Source) then
Exit;

MemStream := Nil;
OutStream := nil;
DecomStream := nil;
try
// 生成流
// 先读文件到 MemStream 中解密(Source 只读).
// DecomStream 的开始位置 = MemStream.Position
MemStream := TMemoryStream.Create;
MemStream.LoadFromFile(Source);
// 在此写解密流代码
// DecryptStream(MemStream);

OutStream := TFileStream.Create(Target, fmCreate or fmShareDenyWrite);
DecomStream := TDeCompressionStream.Create(MemStream);

{  InStream := TFileStream.Create(Source, fmOpenRead or fmShareDenyNone);
OutStream := TFileStream.Create(Target, fmCreate or fmShareDenyWrite);
DecomStream := TDeCompressionStream.Create(InStream); }

// 解压流
repeat
i := DecomStream.Read(Buf, BufSize);
OutStream.WriteBuffer(Buf, i);
until (i = 0);

// 注意先后
OutStream.Free;
OutStream := nil;

DecomStream.Free;
DecomStream := nil;

MemStream.Free;
MemStream := Nil;
except
on E: Exception do begin
if (MemStream <> nil) then
MemStream.Free;
if (OutStream <> nil) then
OutStream.Free;
if (DecomStream <> nil) then
DecomStream.Free;
MessageDlg(DECOMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 压缩文件到流
procedure CompressToStream(FileName: String; Stream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of byte;
ComStream: TCompressionStream;
InStream: TFileStream;
begin
if not FileExists(FileName) then
Exit;
InStream := Nil;
ComStream := nil;
try
// 生成流
InStream := TFileStream.Create(FileName, fmOpenRead OR fmShareDenyNone);
ComStream := TCompressionStream.Create(clMax, Stream);

// 压缩流
for i := 1 to (InStream.Size div BufSize) do begin
InStream.ReadBuffer(Buf, BufSize);
ComStream.Write(Buf, BufSize);
end;

i := InStream.Size mod BufSize;
if (i > 0) then begin
InStream.ReadBuffer(Buf, i);
ComStream.Write(Buf, i);
End;

InStream.Free;
InStream := nil;

ComStream.Free;
ComStream := nil;

// 在此写加密流代码(要先释放 ComStream), Postion = 0
// EncryptStream(Stream);
except
on E: Exception do begin
if (InStream <> nil) then
InStream.Free;
if (ComStream <> nil) then
ComStream.Free;
MessageDlg(COMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 解压文件到流
procedure DecompressToStream(FileName: String; Stream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of Byte;
DecomStream: TDecompressionStream;
MemStream: TMemoryStream;
begin
if not FileExists(FileName) then
Exit;
MemStream := Nil;
DecomStream := nil;
try
// 生成流
// 先读文件到 MemStream 中解密(FileName 只读).
// DecomStream 的开始位置 = MemStream.Position
MemStream := TMemoryStream.Create;
MemStream.LoadFromFile(FileName);

// 在此写解密流代码
// DecryptStream(MemStream);

DecomStream := TDeCompressionStream.Create(MemStream);

// 解压流, Postion = 0
repeat
i := DecomStream.Read(Buf, BufSize);
Stream.WriteBuffer(Buf, i);
until (i = 0);
Stream.Position := 0;

DecomStream.Free;
DecomStream := nil;

MemStream.Free;
MemStream := Nil;
except
on E: Exception do begin
if (MemStream <> nil) then
MemStream.Free;
if (DecomStream <> nil) then
DecomStream.Free;
MessageDlg(DECOMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 压缩流
procedure CompressStream(InStream, OutStream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of byte;
ComStream: TCompressionStream;
begin
ComStream := Nil;
try
InStream.Position := 0;
ComStream := TCompressionStream.Create(clMax, OutStream);

for i := 1 to (InStream.Size div BufSize) do begin
InStream.ReadBuffer(Buf, BufSize);
ComStream.Write(Buf, BufSize);
end;

i := InStream.Size mod BufSize;
if (i > 0) then begin
InStream.ReadBuffer(Buf, i);
ComStream.Write(Buf, i);
End;

ComStream.Free;
ComStream := nil;

// 在此写加密流代码
// EncryptStream(OutStream);
except
on E: Exception do begin
if (ComStream <> nil) then
ComStream.Free;
MessageDlg(COMPRESS_STRM_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 解压流
procedure DecompressStream(InStream, OutStream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of Byte;
DecomStream: TDecompressionStream;
begin
DecomStream := nil;
try
// 在此写解密流代码
// DecryptStream(InStream);
DecomStream := TDeCompressionStream.Create(InStream);

repeat
i := DecomStream.Read(Buf, BufSize);
OutStream.WriteBuffer(Buf, i);
until (i = 0);
OutStream.Position := 0;

DecomStream.Free;
DecomStream := nil;
except
on E: Exception do begin
if (DecomStream <> nil) then
DecomStream.Free;
MessageDlg(DECOMPRESS_STRM_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

end.
(*
* 单元说明: 文件/流的加密解密方法(For Delphi 6-7)
*
* 软件来源: http://www.go-neumann.com/
*      http://www.inbytes.net/
*
* 作  者: [email protected]
*
* 使用说明: 此文件的代码可以直接编译使用,
*      您可以根据需要编写自己的加密/解密代码.
*
* 声  明: 以下代码属作者原创, 请转载时保留本信息.      
*
*)

unit uCompress;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, ComCtrls, Dialogs;

// 压缩/解压文件
procedure CompressFile(Source, Target: String); stdcall;
procedure DecompressFile(Source, Target: String); stdcall;

// 压缩/解压文件到流
procedure CompressToStream(FileName: String; Stream: TStream); stdcall;
procedure DecompressToStream(FileName: String; Stream: TStream); stdcall;

// 压缩/解压流
procedure CompressStream(InStream, OutStream: TStream); stdcall;
procedure DecompressStream(InStream, OutStream: TStream); stdcall;

implementation

uses
ZLib;

const
COMPRESS_ERROR = '压缩文件时出现内部错误:';
DECOMPRESS_ERROR = '解压文件时出现内部错误:';
COMPRESS_STRM_ERROR = '压缩流时出现内部错误:';
DECOMPRESS_STRM_ERROR = '解压流时出现内部错误:';
BufSize = $4096;

// 压缩文件
procedure CompressFile(Source, Target: String);
var
i: Integer;
Buf: array[0..BufSize] of byte;
ComStream: TCompressionStream;
InStream, OutStream: TFileStream;
begin
if not FileExists(Source) then
Exit;
InStream := Nil;
OutStream := nil;
ComStream := nil;
try
// 生成流
InStream := TFileStream.Create(Source, fmOpenRead OR fmShareDenyNone);
OutStream := TFileStream.Create(Target, fmCreate OR fmShareDenyWrite);
ComStream := TCompressionStream.Create(clMax, OutStream);

// 压缩流
for i := 1 to (InStream.Size div BufSize) do begin
InStream.ReadBuffer(Buf, BufSize);
ComStream.Write(Buf, BufSize);
end;

i := InStream.Size mod BufSize;
if (i > 0) then begin
InStream.ReadBuffer(Buf, i);
ComStream.Write(Buf, i);
End;

InStream.Free;
InStream := nil;

// 注先后
ComStream.Free;
ComStream := nil;

// 在此写加密流代码(要先释放 ComStream)
// EncryptStream(OutStream);

OutStream.Free;
OutStream := nil;
except
on E: Exception do begin
if (InStream <> nil) then
InStream.Free;
if (OutStream <> nil) then
OutStream.Free;
if (ComStream <> nil) then
ComStream.Free;
MessageDlg(COMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 解压文件
procedure DecompressFile(Source, Target: String);
var
i: Integer;
Buf: array[0..BufSize] of Byte;
DecomStream: TDecompressionStream;
MemStream: TMemoryStream;
OutStream: TFileStream;
begin
if not FileExists(Source) then
Exit;

MemStream := Nil;
OutStream := nil;
DecomStream := nil;
try
// 生成流
// 先读文件到 MemStream 中解密(Source 只读).
// DecomStream 的开始位置 = MemStream.Position
MemStream := TMemoryStream.Create;
MemStream.LoadFromFile(Source);
// 在此写解密流代码
// DecryptStream(MemStream);

OutStream := TFileStream.Create(Target, fmCreate or fmShareDenyWrite);
DecomStream := TDeCompressionStream.Create(MemStream);

{  InStream := TFileStream.Create(Source, fmOpenRead or fmShareDenyNone);
OutStream := TFileStream.Create(Target, fmCreate or fmShareDenyWrite);
DecomStream := TDeCompressionStream.Create(InStream); }

// 解压流
repeat
i := DecomStream.Read(Buf, BufSize);
OutStream.WriteBuffer(Buf, i);
until (i = 0);

// 注意先后
OutStream.Free;
OutStream := nil;

DecomStream.Free;
DecomStream := nil;

MemStream.Free;
MemStream := Nil;
except
on E: Exception do begin
if (MemStream <> nil) then
MemStream.Free;
if (OutStream <> nil) then
OutStream.Free;
if (DecomStream <> nil) then
DecomStream.Free;
MessageDlg(DECOMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 压缩文件到流
procedure CompressToStream(FileName: String; Stream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of byte;
ComStream: TCompressionStream;
InStream: TFileStream;
begin
if not FileExists(FileName) then
Exit;
InStream := Nil;
ComStream := nil;
try
// 生成流
InStream := TFileStream.Create(FileName, fmOpenRead OR fmShareDenyNone);
ComStream := TCompressionStream.Create(clMax, Stream);

// 压缩流
for i := 1 to (InStream.Size div BufSize) do begin
InStream.ReadBuffer(Buf, BufSize);
ComStream.Write(Buf, BufSize);
end;

i := InStream.Size mod BufSize;
if (i > 0) then begin
InStream.ReadBuffer(Buf, i);
ComStream.Write(Buf, i);
End;

InStream.Free;
InStream := nil;

ComStream.Free;
ComStream := nil;

// 在此写加密流代码(要先释放 ComStream), Postion = 0
// EncryptStream(Stream);
except
on E: Exception do begin
if (InStream <> nil) then
InStream.Free;
if (ComStream <> nil) then
ComStream.Free;
MessageDlg(COMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 解压文件到流
procedure DecompressToStream(FileName: String; Stream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of Byte;
DecomStream: TDecompressionStream;
MemStream: TMemoryStream;
begin
if not FileExists(FileName) then
Exit;
MemStream := Nil;
DecomStream := nil;
try
// 生成流
// 先读文件到 MemStream 中解密(FileName 只读).
// DecomStream 的开始位置 = MemStream.Position
MemStream := TMemoryStream.Create;
MemStream.LoadFromFile(FileName);

// 在此写解密流代码
// DecryptStream(MemStream);

DecomStream := TDeCompressionStream.Create(MemStream);

// 解压流, Postion = 0
repeat
i := DecomStream.Read(Buf, BufSize);
Stream.WriteBuffer(Buf, i);
until (i = 0);
Stream.Position := 0;

DecomStream.Free;
DecomStream := nil;

MemStream.Free;
MemStream := Nil;
except
on E: Exception do begin
if (MemStream <> nil) then
MemStream.Free;
if (DecomStream <> nil) then
DecomStream.Free;
MessageDlg(DECOMPRESS_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 压缩流
procedure CompressStream(InStream, OutStream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of byte;
ComStream: TCompressionStream;
begin
ComStream := Nil;
try
InStream.Position := 0;
ComStream := TCompressionStream.Create(clMax, OutStream);

for i := 1 to (InStream.Size div BufSize) do begin
InStream.ReadBuffer(Buf, BufSize);
ComStream.Write(Buf, BufSize);
end;

i := InStream.Size mod BufSize;
if (i > 0) then begin
InStream.ReadBuffer(Buf, i);
ComStream.Write(Buf, i);
End;

ComStream.Free;
ComStream := nil;

// 在此写加密流代码
// EncryptStream(OutStream);
except
on E: Exception do begin
if (ComStream <> nil) then
ComStream.Free;
MessageDlg(COMPRESS_STRM_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

// 解压流
procedure DecompressStream(InStream, OutStream: TStream);
var
i: Integer;
Buf: array[0..BufSize] of Byte;
DecomStream: TDecompressionStream;
begin
DecomStream := nil;
try
// 在此写解密流代码
// DecryptStream(InStream);
DecomStream := TDeCompressionStream.Create(InStream);

repeat
i := DecomStream.Read(Buf, BufSize);
OutStream.WriteBuffer(Buf, i);
until (i = 0);
OutStream.Position := 0;

DecomStream.Free;
DecomStream := nil;
except
on E: Exception do begin
if (DecomStream <> nil) then
DecomStream.Free;
MessageDlg(DECOMPRESS_STRM_ERROR + #10 + E.Message, mtError, [mbOk], 0);
end;
end;
end;

end.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab 彩色图像、灰度图像、二值图像和索引图像发布时间:2022-07-18
下一篇:
Delphi线程Timer(TThreadTimer)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap