Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPrivKey
LOCAL loRsa
LOCAL lcStrData
LOCAL loBd
LOCAL lcHexSig
LOCAL loPubKey
LOCAL loRsa2
LOCAL loBd2
LOCAL lcOriginalData

lnSuccess = 0

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

loPrivKey = CreateObject('Chilkat.PrivateKey')

* Load the private key from an RSA PEM file:
lnSuccess = loPrivKey.LoadPemFile("private.pem")
IF (lnSuccess = 0) THEN
    ? loPrivKey.LastErrorText
    RELEASE loPrivKey
    CANCEL
ENDIF

loRsa = CreateObject('Chilkat.Rsa')
lnSuccess = loRsa.UsePrivateKey(loPrivKey)
IF (lnSuccess = 0) THEN
    ? loRsa.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    CANCEL
ENDIF

lcStrData = "secret"
loBd = CreateObject('Chilkat.BinData')
loBd.AppendString(lcStrData,"utf-8")

lnSuccess = loRsa.SignRawBd(loBd)
lcHexSig = loBd.GetEncoded("hex")
? lcHexSig

* Recover the data using the corresponding public key:
loPubKey = CreateObject('Chilkat.PublicKey')

* Load the public key from a PEM file:
lnSuccess = loPubKey.LoadFromFile("public.pem")
IF (lnSuccess = 0) THEN
    ? loPubKey.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loBd
    RELEASE loPubKey
    CANCEL
ENDIF

loRsa2 = CreateObject('Chilkat.Rsa')
lnSuccess = loRsa2.UsePublicKey(loPubKey)
IF (lnSuccess = 0) THEN
    ? loRsa2.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loBd
    RELEASE loPubKey
    RELEASE loRsa2
    CANCEL
ENDIF

loBd2 = CreateObject('Chilkat.BinData')
loBd2.AppendEncoded(lcHexSig,"hex")

* Recover the original data.
lnSuccess = loRsa2.VerifyRawBd(loBd2)
IF (lnSuccess = 0) THEN
    ? loRsa2.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loBd
    RELEASE loPubKey
    RELEASE loRsa2
    RELEASE loBd2
    CANCEL
ENDIF

lcOriginalData = loBd2.GetString("utf-8")
? lcOriginalData

RELEASE loPrivKey
RELEASE loRsa
RELEASE loBd
RELEASE loPubKey
RELEASE loRsa2
RELEASE loBd2