![]() ![]() ![]() |
Encrypting a stream
Working with ECL streams, you have a simple way to encrypt a stream data.
Encrypting a stream
There are two ways to encrypt a stream data:
First way could be used when:
Second variant could be used to:
Opening an encrypted stream
To access an encrypted data through the ECL stream (TECLFileStream or TECLStream) you should pass proper Password parameter to a stream constructor.
If the value of Password parameter is wrong, an exception is raised.
Accessing encrypted stream data
Once you created ECL stream object with the right password, you may don't care about the encryption. ECL streams encrypt and decrypt the stream data on the fly.
Decrypting a stream
To decrypt data of already created TECLFileStream or TECLMemoryStream (with the right Password) you should set Encrypted property of ECL stream to False.
See also Encrypting a file, Getting encryption state of a file topics.
The following example illustrates how to encrypt and compress and then decrypt and decompress a file.
Example:
var
CompFS: TECLFileStream;
begin
{ create compressed and encrypted file }
CompFS := TECLFileStream.Create('c:\test_comp.ecl', fmCreate, 'Password', zlibFastest);
{ load, compress and encrypt data from test.txt file}
CompFS.LoadFromFile('c:\test.txt');
{ show the size of compressed data }
ShowMessage('Size of compressed and encrypted file: '+IntToStr(CompFS.PackedSize));
{ close compressed and encrypted file }
CompFS.Free;
{ open compressed and encrypted file }
CompFS := TECLFileStream.Create('c:\test_comp.ecl', fmOpenReadWrite or fmShareDenyNone, 'Password');
{ switch off an encryption }
CompFS.Encrypted := False;
{ close compressed and decrypted file }
CompFS.Free;
end;