Xceed Encryption Library Documentation
Encrypting and decrypting from file to memory (Delphi streaming example)
Examples > Encrypting and decrypting from file to memory (Delphi streaming example)
Delphi Copy Code

{ This example encrypt data using the Rijndael method from a file to memory in 8K (8192 byte) chunks. } 

uses XceedEncryptionLib_TLB
const CHUNKSIZE = 8192; 

var
  xEncrypt : TXceedEncryption; 
  xRijndael : DXceedRijndaelEncryptionMethod; 
  vaBytesRead : OleVariant; 
  vaEncrypted : OleVariant; 
  lOffset : Longint; 
  lFileSize : Longint; 
  SourceFile : file of byte; 
begin
  xEncrypt := TXceedEncryption.Create( self ); 
  xEncrypt.License( 'your license key' ); 

  xRijndael := CoXceedRijndaelEncryptionMethod.Create(); 

  try 
    xRijndael.SetSecretKeyFromPassPhrase( 'This is a weak pass phrase!', 128 ); 

    xEncrypt.EncryptionMethod := xRijndael;  

    AssignFile( SourceFile, 'c:\temp\source.txt' ); 

    Reset( SourceFile ); 

    lFileSize := FileSize( SourceFile ); 

    CloseFile( SourceFile ); 

    lOffset := 0;  

    while lOffset < lFileSize do 
    begin 
     { bEndOfData will be true if the current offset + CHUNKSIZE exceeds the end of the file. } 

     vaEncrypted := xEncrypt.ReadFile( 'c:\temp\source.txt', lOffset, CHUNKSIZE, efpEncrypt, 
                                       (lOffset + CHUNKSIZE >= lFileSize), vaBytesRead );  

     lOffset := lOffset + vaBytesRead;  

     if VarType(vaEncrypted) <> varEmpty then 
     begin 
       // You now have a portion of encrypted data you can use. 
       // For example, transfer it, process it, etc. 
       lOffset := lOffset + 1 - 1; 
     end; 
   end;  

   ShowMessage( 'Encryption successful!' ); 
  except 
    on xErr : Exception do 
      ShowMessage( xErr.Message ); 
  end;  

  xEncrypt.Free; 
end;