Discuss this help topic in SecureBlackbox Forum

Decrypt document

To decrypt a binary document you need to

  1. load the document into an instance of TElOfficeBinaryDocument class
  2. check the document format and whether the document is encrypted
  3. check the type of the encryption handler by inspecting TElOfficeBinaryDocument.EncryptionHandler property and checking the type of the referenced object
  4. cast the encryption handler to its real type, and set the Password property
  5. check validity of the password
  6. call TElOfficeBinaryDocument.DecryptTo() method to decrypt the document and save it to a stream.

C#:


void DecryptBinaryRC4(string sourceFilename, string destFilename, string password)
{
    using (TElOfficeDocument Document = new TElOfficeDocument())
    {
        Document.Open(sourceFilename);
        if ((Document.DocumentFormat != TSBOfficeDocumentFormat.Binary) || !Document.IsEncrypted)
            throw new Exception("Cannot decrypt document using Binary RC4 encryption handler");

        if (Document.EncryptionHandler is TElOfficeBinaryRC4CryptoAPIEncryptionHandler)
        {
            TElOfficeBinaryRC4CryptoAPIEncryptionHandler RC4CryptoAPIEncryptionHandler = (TElOfficeBinaryRC4CryptoAPIEncryptionHandler)Document.EncryptionHandler;
            RC4CryptoAPIEncryptionHandler.Password = password;
            if (!RC4CryptoAPIEncryptionHandler.IsPasswordValid())
                throw new Exception("Invalid password");
        }
        else if (Document.EncryptionHandler is TElOfficeBinaryRC4EncryptionHandler)
        {
            TElOfficeBinaryRC4EncryptionHandler RC4EncryptionHandler = (TElOfficeBinaryRC4EncryptionHandler)Document.EncryptionHandler;
            RC4EncryptionHandler.Password = password;
            if (!RC4EncryptionHandler.IsPasswordValid())
                throw new Exception("Invalid password");
        }
        else
            throw new Exception("Unknown encryption handler");

        using (FileStream f = new FileStream(destFilename, FileMode.CreateNew))
        {
            Document.DecryptTo(f);
        }
    }
}
Delphi:

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

    if Document.EncryptionHandler is TElOfficeBinaryRC4CryptoAPIEncryptionHandler then
    begin
      TElOfficeBinaryRC4CryptoAPIEncryptionHandler(Document.EncryptionHandler).Password := Password;
      if not TElOfficeBinaryRC4CryptoAPIEncryptionHandler(Document.EncryptionHandler).IsPasswordValid then
        raise Exception.Create('Invalid password');
    end
    else if Document.EncryptionHandler is TElOfficeBinaryRC4EncryptionHandler then
    begin
      TElOfficeBinaryRC4EncryptionHandler(Document.EncryptionHandler).Password := Password;
      if not TElOfficeBinaryRC4EncryptionHandler(Document.EncryptionHandler).IsPasswordValid then
        raise Exception.Create('Invalid password');
    end
    else
      raise Exception.Create('Unknown encryption handler');

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

How To articles about MS Office binary documents

Discuss this help topic in SecureBlackbox Forum