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

RSA Sign using Base64 Private Key

See more RSA Examples

Signs a string using a non-encrypted RSA private key in base64 encoding. Returns the RSA signature as a base64 string.

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.StringBuilder,
  Chilkat.PrivateKey,
  Chilkat.Rsa,
  Chilkat.BinData;

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

procedure RunDemo;
var
  success: Boolean;
  privKey: TPrivateKey;
  sbPem: TStringBuilder;
  rsa: TRsa;
  bd: TBinData;
  strOriginal: string;

begin
  success := False;

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

  privKey := TPrivateKey.Create;

  sbPem := TStringBuilder.Create;
  sbPem.AppendLine('-----BEGIN RSA PRIVATE KEY-----',True);
  sbPem.AppendLine('MIIC .... j5A==',True);
  sbPem.AppendLine('-----END RSA PRIVATE KEY-----',True);

  success := privKey.LoadPem(sbPem.GetAsString());
  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;

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

  success := rsa.SignRawBd(bd);
  if (success = False) then
    begin
      WriteLn(rsa.LastErrorText);
      Exit;
    end;

  //  Get the base64 RSA signature.
  WriteLn(bd.GetEncoded('base64'));

  success := rsa.VerifyRawBd(bd);
  if (success = False) then
    begin
      WriteLn(rsa.LastErrorText);
      Exit;
    end;

  strOriginal := bd.GetString('utf-8');
  WriteLn(strOriginal);


  privKey.Free;
  sbPem.Free;
  rsa.Free;
  bd.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.