// 司马华鹏
//------------------------------------------------------------------------------
//解密模块
function TUserInfoAccess.myDecryptStream(const Source:TStream;
Dest: TStream;
Password:string):Boolean;
var
Buffer: array[0..8191] of byte;
Hash: TDCP_sha1;
HashDigest, HashRead: array[0..31] of byte;
Decrypt: TDCP_blockcipher;
Read: integer;
Blowfish:TDCP_blowfish;
begin
Result:=True;
try
FillChar(HashDigest,Sizeof(HashDigest),$FF);
// fill the digest with $FF as the actual digest may not fill the entire digest
Hash:= TDCP_sha1.Create(nil);
Hash.Init;
// hash the passphrase to get the key
Hash.UpdateStr(Password);
Hash.Final(HashDigest);
Hash.Free;
Blowfish:=TDCP_blowfish.Create(nil);
Decrypt:= TDCP_blockcipher(TComponent(Blowfish));
// get the component from the combo box
if (Sizeof(HashDigest)*8)> Decrypt.MaxKeySize then
Decrypt.Init(HashDigest,Decrypt.MaxKeySize,nil) // make sure the key isn't too big
else
Decrypt.Init(HashDigest,Sizeof(HashDigest)*8,nil);
// initialize the cipher with the digest, IV= nil to generate one automatically (note: size in bits ie. sizeof(x)*8)
try
try
Decrypt.EncryptCBC(HashDigest,HashDigest,Sizeof(HashDigest));
// encrypt the hash to use as confirmation
Decrypt.Reset;
Source.ReadBuffer(HashRead,Sizeof(HashRead));
// read the other hash from the file and compare it
if not CompareMem(@HashRead,@HashDigest,Sizeof(HashRead)) then
Exception.Create('Failed to Decrypt');
repeat
Read:=Source.Read(Buffer,Sizeof(Buffer));
Decrypt.DecryptCBC(Buffer,Buffer,Read);
// read from the source decrypt and write out the result
Dest.WriteBuffer(Buffer,Read);
until Read<> Sizeof(Buffer);
finally
Decrypt.Burn;
// burn the key data (note:do
n't free it as we placed it on the form at design time)
Blowfish.Free;
end;
except
On e:exceptiondo
begin
Result:=False;
end;
end;
except
On e:exceptiondo
begin
Result:=False;
end;
end;
end;
//------------------------------------------------------------------------------
//加密文件单元
function TUserInfoAccess.myEncryptStream(const Source:TStream;
Dest: TStream;
Password:string):Boolean;
var
Buffer: array[0..8191] of byte;
Hash: TDCP_sha1;
HashDigest: array[0..31] of byte;
Encrypt: TDCP_blockcipher;
Read: integer;
Blowfish:TDCP_blowfish;
begin
Result:=True;
try
FillChar(HashDigest,Sizeof(HashDigest),$FF);
// fill the digest with $FF as the actual digest may not fill the entire digest
Hash:= TDCP_sha1.Create(nil);
Hash.Init;
Hash.UpdateStr(Password);
// hash the passphrase to get the key
Hash.Final(HashDigest);
Hash.Free;
Blowfish:=TDCP_blowfish.Create(nil);
Encrypt:= TDCP_blockcipher(TComponent(Blowfish));
// get the component from the combo box
try
try
if (Sizeof(HashDigest)*8)> Encrypt.MaxKeySize then
Encrypt.Init(HashDigest,Encrypt.MaxKeySize,nil) // make sure the key isn't too big
else
Encrypt.Init(HashDigest,Sizeof(HashDigest)*8,nil);
// initialize the cipher with the digest, IV= nil to generate one automatically (note: size in bits ie. sizeof(x)*8)
Encrypt.EncryptCBC(HashDigest,HashDigest,Sizeof(HashDigest));
// encrypt the hash and write it to the file to use as passphrase
Encrypt.Reset;
// confirmation
Dest.Write(HashDigest,Sizeof(HashDigest));
repeat
Read:=Source.Read(Buffer,Sizeof(Buffer));
Encrypt.EncryptCBC(Buffer,Buffer,Read);
// read from the source encrypt and write out the result
Dest.Write(Buffer,Read);
until Read<>Sizeof(Buffer);
finally
Encrypt.Burn;
// burn the key data (note:do
n't free it as we placed it on the form at design time)
Blowfish.Free;
end;
except
on e:exceptiondo
begin
Result:=False;
end;
end;
except
on e:exceptiondo
begin
Result:=False;
end;
end;
end;