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

Duplicate OpensSSL Command that Decrypts Binary DER

See more OpenSSL Examples

This example duplicates the following:
openssl smime -decrypt -in INPUT_FILE -inform der -binary -out OUTPUT_FILE -recip PEM_CERT_AND_KEY -passin pass:PRIVKEY_PASSWORD

Note: Although "smime" is the OpenSSL command, we're not really dealing with S/MIME. The arguments "-inform der -binary" indicate that the input is simply the binary DER (i.e. the PKCS7 binary encrypted object). The output can be any type of file (whatever was encrypted).

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.PrivateKey,
  Chilkat.Cert,
  Chilkat.Pem,
  Chilkat.Crypt2;

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

procedure RunDemo;
var
  success: Boolean;
  crypt: TCrypt2;
  pem: TPem;
  privkey: TPrivateKey;
  cert: TCert;

begin
  success := False;

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

  crypt := TCrypt2.Create;

  crypt.CryptAlgorithm := 'pki';

  pem := TPem.Create;
  success := pem.LoadPemFile('qa_data/pem/myPem.pem','password');
  if (success = False) then
    begin
      WriteLn(pem.LastErrorText);
      Exit;
    end;

  privkey := TPrivateKey.Create;
  success := pem.PrivateKeyAt(0,privkey);
  if (success = False) then
    begin
      WriteLn(pem.LastErrorText);
      Exit;
    end;

  cert := TCert.Create;
  success := pem.CertAt(0,cert);
  if (success = False) then
    begin
      WriteLn(pem.LastErrorText);
      Exit;
    end;

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

  success := crypt.CkDecryptFile('qa_data/infile.enc','qa_output/outfile.dat');
  if (success = False) then
    begin
      WriteLn(crypt.LastErrorText);
      Exit;
    end;

  WriteLn('Success.');


  crypt.Free;
  pem.Free;
  privkey.Free;
  cert.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.