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

Duplicating OpenSSL rsautl (creating RSA signatures)

See more RSA Examples

Demonstrates how to duplicate OpenSSL rsautil RSA signatures.

The Chilkat RSA component's methods for creating RSA signatures (SignBytes, SignBytesENC, SignString, and SignStringENC) are very different from OpenSSL's rsautl command. First, we'll explain what Chilkat's signing methods do, and then what OpenSSL's rsautl does. New signing methods have been added to Chilkat RSA to duplicate OpenSSL rsautl: OpenSslSignBytes, OpenSslSignBytesENC, OpenSslSignString, and OpenSslSignStringENC.

Here's what Chilkat's RSA Sign* methods do:
(Note: Chilkat RSA's Sign* methods generate signatures according to RSA Laboratories' "PKCS #1 v2.0: RSA Cryptography Standard".)

  1. Input data is hashed using the hashing algorithm specified.
  2. The hash is padded using either PKCS1 v1.5 or PKCS1 PSS padding. PKCS v1.5 involves encoding to ASN.1 before padding.
  3. The padded hash is finally RSA signed (i.e. modular exponentiation) and the signature is returned.

OpenSSL rsautl is very different. Here's what it does:

  1. The input data is not hashed. It must be a small enough amount of data such that it can be padded and signed. PKCS v1.5 padding is always used. The data is not ASN.1 encoded before padding.
  2. The PKCS v1.5 padded data is RSA signed and the signature is returned.
The new OpenSsl* methods are designed to duplicate OpenSSL's rsautil signature functionality.

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.Rsa,
  Chilkat.PublicKey;

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

procedure RunDemo;
var
  success: Boolean;
  privKey: TPrivateKey;
  rsa: TRsa;
  strData: string;
  bd: TBinData;
  hexSig: string;
  pubKey: TPublicKey;
  rsa2: TRsa;
  bd2: TBinData;
  originalData: string;

begin
  success := False;

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

  privKey := TPrivateKey.Create;

  //  Load the private key from an RSA PEM file:
  success := privKey.LoadPemFile('private.pem');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  rsa := TRsa.Create;
  success := rsa.UsePrivateKey(privKey);
  if (success = False) then
    begin
      WriteLn(rsa.LastErrorText);
      Exit;
    end;

  strData := 'secret';
  bd := TBinData.Create;
  bd.AppendString(strData,'utf-8');

  success := rsa.SignRawBd(bd);
  hexSig := bd.GetEncoded('hex');
  WriteLn(hexSig);

  //  Recover the data using the corresponding public key:
  pubKey := TPublicKey.Create;

  //  Load the public key from a PEM file:
  success := pubKey.LoadFromFile('public.pem');
  if (success = False) then
    begin
      WriteLn(pubKey.LastErrorText);
      Exit;
    end;

  rsa2 := TRsa.Create;
  success := rsa2.UsePublicKey(pubKey);
  if (success = False) then
    begin
      WriteLn(rsa2.LastErrorText);
      Exit;
    end;

  bd2 := TBinData.Create;
  bd2.AppendEncoded(hexSig,'hex');

  //  Recover the original data.
  success := rsa2.VerifyRawBd(bd2);
  if (success = False) then
    begin
      WriteLn(rsa2.LastErrorText);
      Exit;
    end;

  originalData := bd2.GetString('utf-8');
  WriteLn(originalData);


  privKey.Free;
  rsa.Free;
  bd.Free;
  pubKey.Free;
  rsa2.Free;
  bd2.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.