Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, BinData, PrivateKey, Rsa, PublicKey;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
privKey: HCkPrivateKey;
rsa: HCkRsa;
strData: PWideChar;
bd: HCkBinData;
hexSig: PWideChar;
pubKey: HCkPublicKey;
rsa2: HCkRsa;
bd2: HCkBinData;
originalData: PWideChar;

begin
success := False;

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

privKey := CkPrivateKey_Create();

// Load the private key from an RSA PEM file:
success := CkPrivateKey_LoadPemFile(privKey,'private.pem');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPrivateKey__lastErrorText(privKey));
    Exit;
  end;

rsa := CkRsa_Create();
success := CkRsa_UsePrivateKey(rsa,privKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRsa__lastErrorText(rsa));
    Exit;
  end;

strData := 'secret';
bd := CkBinData_Create();
CkBinData_AppendString(bd,strData,'utf-8');

success := CkRsa_SignRawBd(rsa,bd);
hexSig := CkBinData__getEncoded(bd,'hex');
Memo1.Lines.Add(hexSig);

// Recover the data using the corresponding public key:
pubKey := CkPublicKey_Create();

// Load the public key from a PEM file:
success := CkPublicKey_LoadFromFile(pubKey,'public.pem');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPublicKey__lastErrorText(pubKey));
    Exit;
  end;

rsa2 := CkRsa_Create();
success := CkRsa_UsePublicKey(rsa2,pubKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRsa__lastErrorText(rsa2));
    Exit;
  end;

bd2 := CkBinData_Create();
CkBinData_AppendEncoded(bd2,hexSig,'hex');

// Recover the original data.
success := CkRsa_VerifyRawBd(rsa2,bd2);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRsa__lastErrorText(rsa2));
    Exit;
  end;

originalData := CkBinData__getString(bd2,'utf-8');
Memo1.Lines.Add(originalData);

CkPrivateKey_Dispose(privKey);
CkRsa_Dispose(rsa);
CkBinData_Dispose(bd);
CkPublicKey_Dispose(pubKey);
CkRsa_Dispose(rsa2);
CkBinData_Dispose(bd2);

end;