procedure TForm1.FormCreate(Sender: TObject);
var
Cipher: TDCP_cipher; // the cipher to use
CipherIV: array of byte; // the initialisation vector (for chaining modes)
Hash: TDCP_hash; // the hash to use
HashDigest: array of byte; // the result of hashing the passphrase with the salt
Salt: array[0..7] of byte; // a random salt to help prevent precomputated attacks
strmInput: TFileStream;
strmOutput: TMemoryStream;
begin
strmInput := nil;
strmOutput := nil;
try
strmInput := TFileStream.Create('data/encrypt.db',fmOpenRead);
strmOutput := TMemoryStream.Create;
Hash := TDCP_hash(DCP_haval1);
SetLength(HashDigest,Hash.HashSize div 8);
strmInput.ReadBuffer(Salt[0],Sizeof(Salt)); // read the salt in from the file
Hash.Init;
Hash.Update(Salt[0],Sizeof(Salt)); // hash the salt
Hash.UpdateStr('pass'); // and the passphrase
Hash.Final(HashDigest[0]); // store the hash in HashDigest
Cipher := TDCP_cipher(DCP_blowfish1);
if (Cipher is TDCP_blockcipher) then // if it is a block cipher we need the IV
begin
SetLength(CipherIV,TDCP_blockcipher(Cipher).BlockSize div 8);
strmInput.ReadBuffer(CipherIV[0],Length(CipherIV)); // read the initialisation vector from the file
Cipher.Init(HashDigest[0],Min(Cipher.MaxKeySize,Hash.HashSize),CipherIV); // initialise the cipher
TDCP_blockcipher(Cipher).CipherMode := cmCBC;
end
else
Cipher.Init(HashDigest[0],Min(Cipher.MaxKeySize,Hash.HashSize),nil); // initialise the cipher
Cipher.DecryptStream(strmInput,strmOutput,strmInput.Size - strmInput.Position); // decrypt!
Cipher.Burn;
strmInput.Free;
// strmOutput.Free;
MessageDlg('File decrypted',mtInformation,[mbOK],0);
except
strmInput.Free;
strmOutput.Free;
MessageDlg('Ошибка при расшифровке файла базы данных, обратитесь к разработчику.',mtError,[mbOK],0);
end;
end;