📄 lbproc.pas
字号:
InitEncryptDES(Key, Context, Encrypt);
{get the number of blocks in the file}
BlockCount := (InStream.Size div SizeOf(Block));
{when encrypting, make sure we have a block with at least one free}
{byte at the end. used to store the odd byte count value}
if Encrypt then
Inc(BlockCount);
{process all except the last block}
for I := 1 to BlockCount - 1 do begin
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
EncryptDES(Context, Block);
OutStream.Write(Block, SizeOf(Block));
if Assigned(LbOnProgress) then {!!.06a}
if InStream.Position mod LbProgressSize = 0 then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
if Encrypt then begin
FillChar(Block, SizeOf(Block), #0);
{set actual number of bytes to read}
I := (InStream.Size mod SizeOf(Block));
if InStream.Read(Block, I) <> I then
raise ECipherException.Create(SInvalidFileFormat);
{store number of bytes as last byte in last block}
PByteArray(@Block)^[SizeOf(Block)-1] := I;
{encrypt and save full block}
EncryptDES(Context, Block);
OutStream.Write(Block, SizeOf(Block));
end else begin
{encrypted file is always a multiple of the block size}
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
EncryptDES(Context, Block);
{get actual number of bytes encoded}
I := PByteArray(@Block)^[SizeOf(Block)-1];
{save valid portion of block}
OutStream.Write(Block, I);
end;
if Assigned(LbOnProgress) then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
{ -------------------------------------------------------------------------- }
procedure DESEncryptStreamCBC(InStream, OutStream : TStream;
const Key : TKey64; Encrypt : Boolean);
var
I : LongInt;
Block : TDESBlock;
IV : TDESBlock;
Work : TDESBlock;
Context : TDESContext;
BlockCount : LongInt;
{$IFDEF LINUX}
fd : pIOFile;
{$ENDIF}
begin
InitEncryptDES(Key, Context, Encrypt);
{get the number of blocks in the file}
BlockCount := (InStream.Size div SizeOf(Block));
if Encrypt then begin
{set up an initialization vector (IV)}
{$IFDEF MSWINDOWS}
Block[0] := timeGetTime;
Block[1] := timeGetTime;
Block[2] := timeGetTime;
Block[3] := timeGetTime;
{$ENDIF}
{$IFDEF LINUX}
fd := fopen( '/dev/random', 'r' );
fread( @Block[0], SizeOf( byte ), SizeOf( Block[0] ), fd );
fread( @Block[1], SizeOf( byte ), SizeOf( Block[1] ), fd );
fread( @Block[2], SizeOf( byte ), SizeOf( Block[2] ), fd );
fread( @Block[3], SizeOf( byte ), SizeOf( Block[3] ), fd );
fclose( fd );
{$ENDIF}
EncryptDES(Context, Block);
OutStream.Write(Block, SizeOf(Block));
IV := Block;
end else begin
{read the frist block to prime the IV}
InStream.Read(Block, SizeOf(Block));
Dec(BlockCount);
IV := Block;
end;
{when encrypting, make sure we have a block with at least one free}
{byte at the end. used to store the odd byte count value}
if Encrypt then
Inc(BlockCount);
{process all except the last block}
for I := 1 to BlockCount - 1 do begin
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
if Encrypt then begin
EncryptDESCBC(Context, IV, Block);
IV := Block;
end else begin
Work := Block;
EncryptDESCBC(Context, IV, Block);
IV := Work;
end;
OutStream.Write(Block, SizeOf(Block));
if Assigned(LbOnProgress) then {!!.06a}
if InStream.Position mod LbProgressSize = 0 then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
if Encrypt then begin
FillChar(Block, SizeOf(Block), #0);
{set actual number of bytes to read}
I := (InStream.Size mod SizeOf(Block));
if InStream.Read(Block, I) <> I then
raise ECipherException.Create(SInvalidFileFormat);
{store number of bytes as last byte in last block}
PByteArray(@Block)^[SizeOf(Block)-1] := I;
{encrypt and save full block}
EncryptDESCBC(Context, IV, Block);
OutStream.Write(Block, SizeOf(Block));
end else begin
{encrypted file is always a multiple of the block size}
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
EncryptDESCBC(Context, IV, Block);
{get actual number of bytes encoded}
I := PByteArray(@Block)^[SizeOf(Block)-1];
{save valid portion of block}
OutStream.Write(Block, I);
end;
if Assigned(LbOnProgress) then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
{ == LockBox Cipher ======================================================== }
procedure LBCEncryptFile(const InFile, OutFile : string;
const Key : TKey128; Rounds : LongInt; Encrypt : Boolean);
var
InStream, OutStream : TStream;
begin
InStream := TFileStream.Create(InFile, fmOpenRead or fmShareDenyWrite);
try
OutStream := TFileStream.Create(OutFile, fmCreate);
try
LBCEncryptStream(InStream, OutStream, Key, Rounds, Encrypt);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
end;
{ -------------------------------------------------------------------------- }
procedure LBCEncryptFileCBC(const InFile, OutFile : string;
const Key : TKey128; Rounds : LongInt; Encrypt : Boolean);
var
InStream, OutStream : TStream;
begin
InStream := TFileStream.Create(InFile, fmOpenRead or fmShareDenyWrite);
try
OutStream := TFileStream.Create(OutFile, fmCreate);
try
LBCEncryptStreamCBC(InStream, OutStream, Key, Rounds, Encrypt);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
end;
{ -------------------------------------------------------------------------- }
procedure LBCEncryptStream(InStream, OutStream : TStream;
const Key : TKey128; Rounds : LongInt; Encrypt : Boolean);
var
I : LongInt;
Block : TLBCBlock;
Context : TLBCContext;
BlockCount : LongInt;
begin
InitEncryptLBC(Key, Context, Rounds, Encrypt);
{get the number of blocks in the file}
BlockCount := (InStream.Size div SizeOf(Block));
{when encrypting, make sure we have a block with at least one free}
{byte at the end. used to store the odd byte count value}
if Encrypt then
Inc(BlockCount);
{process all except the last block}
for I := 1 to BlockCount - 1 do begin
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
EncryptLBC(Context, Block);
OutStream.Write(Block, SizeOf(Block));
if Assigned(LbOnProgress) then {!!.06a}
if InStream.Position mod LbProgressSize = 0 then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
if Encrypt then begin
FillChar(Block, SizeOf(Block), #0);
{set actual number of bytes to read}
I := (InStream.Size mod SizeOf(Block));
if InStream.Read(Block, I) <> I then
raise ECipherException.Create(SInvalidFileFormat);
{store number of bytes as last byte in last block}
PByteArray(@Block)^[SizeOf(Block)-1] := I;
{encrypt and save full block}
EncryptLBC(Context, Block);
OutStream.Write(Block, SizeOf(Block));
end else begin
{encrypted file is always a multiple of the block size}
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
EncryptLBC(Context, Block);
{get actual number of bytes encoded}
I := PByteArray(@Block)^[SizeOf(Block)-1];
{save valid portion of block}
OutStream.Write(Block, I);
end;
if Assigned(LbOnProgress) then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
{ -------------------------------------------------------------------------- }
procedure LBCEncryptStreamCBC(InStream, OutStream : TStream;
const Key : TKey128; Rounds : LongInt; Encrypt : Boolean);
var
I : LongInt;
Block : TLBCBlock;
IV : TLBCBlock;
Work : TLBCBlock;
Context : TLBCContext;
BlockCount : LongInt;
{$IFDEF LINUX}
fd : pIOFile;
{$ENDIF}
begin
InitEncryptLBC(Key, Context, Rounds, Encrypt);
{get the number of blocks in the file}
BlockCount := (InStream.Size div SizeOf(Block));
if Encrypt then begin
{set up an initialization vector (IV)}
{$IFDEF MSWINDOWS}
Block[0] := timeGetTime;
Block[1] := timeGetTime;
Block[2] := timeGetTime;
Block[3] := timeGetTime;
{$ENDIF}
{$IFDEF LINUX}
fd := fopen( '/dev/random', 'r' );
fread( @Block[0], SizeOf( byte ), SizeOf( Block[0] ), fd );
fread( @Block[1], SizeOf( byte ), SizeOf( Block[1] ), fd );
fread( @Block[2], SizeOf( byte ), SizeOf( Block[2] ), fd );
fread( @Block[3], SizeOf( byte ), SizeOf( Block[3] ), fd );
fclose( fd );
{$ENDIF}
EncryptLBC(Context, Block);
OutStream.Write(Block, SizeOf(Block));
IV := Block;
end else begin
{read the frist block to prime the IV}
InStream.Read(Block, SizeOf(Block));
Dec(BlockCount);
IV := Block;
end;
{when encrypting, make sure we have a block with at least one free}
{byte at the end. used to store the odd byte count value}
if Encrypt then
Inc(BlockCount);
{process all except the last block}
for I := 1 to BlockCount - 1 do begin
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
if Encrypt then begin
EncryptLBCCBC(Context, IV, Block);
IV := Block;
end else begin
Work := Block;
EncryptLBCCBC(Context, IV, Block);
IV := Work;
end;
OutStream.Write(Block, SizeOf(Block));
if Assigned(LbOnProgress) then {!!.06a}
if InStream.Position mod LbProgressSize = 0 then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
if Encrypt then begin
FillChar(Block, SizeOf(Block), #0);
{set actual number of bytes to read}
I := (InStream.Size mod SizeOf(Block));
if InStream.Read(Block, I) <> I then
raise ECipherException.Create(SInvalidFileFormat);
{store number of bytes as last byte in last block}
PByteArray(@Block)^[SizeOf(Block)-1] := I;
{encrypt and save full block}
EncryptLBCCBC(Context, IV, Block);
OutStream.Write(Block, SizeOf(Block));
end else begin
{encrypted file is always a multiple of the block size}
if InStream.Read(Block, SizeOf(Block)) <> SizeOf(Block) then
raise ECipherException.Create(SInvalidFileFormat);
EncryptLBCCBC(Context, IV, Block);
{get actual number of bytes encoded}
I := PByteArray(@Block)^[SizeOf(Block)-1];
{save valid portion of block}
OutStream.Write(Block, I);
end;
if Assigned(LbOnProgress) then {!!.06a}
LbOnProgress (InStream.Position, InStream.Size); {!!.06a}
end;
{ == LockBox Quick Cipher (LQC) ============================================ }
procedure LQCEncryptFile(const InFile, OutFile : string;
const Key : TKey128; Encrypt : Boolean);
var
InStream, OutStream : TStream;
begin
InStream := TFileStream.Create(InFile, fmOpenRead or fmShareDenyWrite);
try
OutStream := TFileStream.Create(OutFile, fmCreate);
try
LQCEncryptStream(InStream, OutStream, Key, Encrypt);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -