Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Duplicate openssl smime -decrypt -in some_file.dat.enc -binary -inform DER -inkey private.key -out some_file.dat

See more OpenSSL Examples

Demonstrates how to decrypt binary DER that was encrypted using the following openssl command:
openssl smime -encrypt -binary -aes-256-cbc -in some_file.dat -out some_file.dat.enc -outform DER cert.crt

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.PrivateKey,
  Chilkat.Cert,
  Chilkat.Crypt2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  bd: TBinData;
  privKey: TPrivateKey;
  crypt: TCrypt2;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  //  Duplicates the following openssl command:
  //  openssl smime -decrypt -in hello.txt.enc -binary -inform DER -inkey private.key -out hello.txt

  cert := TCert.Create;
  success := cert.LoadFromFile('qa_data/openssl/EE.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  bd := TBinData.Create;
  success := bd.LoadFile('qa_data/openssl/EE.key');
  //  Assuming success..

  privKey := TPrivateKey.Create;
  success := privKey.LoadAnyFormat(bd,'');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  crypt := TCrypt2.Create;
  success := crypt.SetDecryptCert2(cert,privKey);
  if (success = False) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  crypt.CryptAlgorithm := 'PKI';
  success := crypt.CkDecryptFile('qa_data/openssl/hello.txt.enc','qa_output/hello.txt');
  if (success = False) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  WriteLn('Success.');


  cert.Free;
  bd.Free;
  privKey.Free;
  crypt.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.