Discuss this help topic in SecureBlackbox Forum

Encrypt document with CryptoAPI ARCFOUR encryption handler

To encrypt a binary document using CryptoAPI RC4 encryption handler you need to

  1. load the document into an instance of TElOfficeBinaryDocument class
  2. if the document is encryptable, create an instance of TElOfficeBinaryRC4CryptoAPIEncryptionHandler class
  3. set Password property and other encryption-related properties of the created encryption handler instance
  4. assign this encryption handler to TElOfficeDocument.EncryptionHandler property
  5. call TElOfficeBinaryDocument.EncryptTo() method to encrypt and save the document to a stream.

C#:


void EncryptBinaryRC4CryptoAPI(string sourceFilename, string destFilename, string password)
{
    TElOfficeDocument Document = new TElOfficeDocument();
    TElOfficeBinaryRC4CryptoAPIEncryptionHandler RC4CryptoAPIEncryptionHandler = new TElOfficeBinaryRC4CryptoAPIEncryptionHandler();
    try
    {
        Document.Open(sourceFilename);
        if ((Document.DocumentFormat != TSBOfficeDocumentFormat.Binary) || !Document.Encryptable)
            throw new Exception("Cannot encrypt document using Binary RC4 CryptoAPI encryption handler");

        RC4CryptoAPIEncryptionHandler.Password = password;
        RC4CryptoAPIEncryptionHandler.KeyLength = 128;
        RC4CryptoAPIEncryptionHandler.HardenedKeyGeneration = true;
        RC4CryptoAPIEncryptionHandler.EncryptDocumentProperties = false;
        Document.EncryptionHandler = RC4CryptoAPIEncryptionHandler;

        using (FileStream f = new FileStream(destFilename, FileMode.CreateNew))
        {
            Document.EncryptTo(f);
        }
    }
    finally
    {
        Document.Dispose();
        RC4CryptoAPIEncryptionHandler.Dispose();
    }
}
Delphi:

procedure EncryptBinaryRC4CryptoAPI(const SourceFilename, DestFilename, Password : string);
var
  Document : TElOfficeDocument;
  RC4CryptoAPIEncryptionHandler : TElOfficeBinaryRC4CryptoAPIEncryptionHandler;
  F : TFileStream;
begin
  Document := TElOfficeDocument.Create(nil);
  RC4CryptoAPIEncryptionHandler := TElOfficeBinaryRC4CryptoAPIEncryptionHandler.Create(nil);
  try
    Document.Open(SourceFilename);
    if (Document.DocumentFormat <> dfBinary) or not Document.Encryptable then
      raise Exception.Create('Cannot encrypt document using Binary RC4 CryptoAPI encryption handler');

    RC4CryptoAPIEncryptionHandler.Password := Password;
    RC4CryptoAPIEncryptionHandler.KeyLength := 128;
    RC4CryptoAPIEncryptionHandler.HardenedKeyGeneration := true;
    RC4CryptoAPIEncryptionHandler.EncryptDocumentProperties := false;
    Document.EncryptionHandler := RC4CryptoAPIEncryptionHandler;

    F := TFileStream.Create(DestFilename, fmCreate or fmShareDenyWrite);
    try
      Document.EncryptTo(F);
    finally
      FreeAndNil(F);
    end;
  finally
    FreeAndNil(Document);
    FreeAndNil(RC4CryptoAPIEncryptionHandler);
  end;
end;

How To articles about MS Office binary documents

Discuss this help topic in SecureBlackbox Forum