Sample code for 30+ languages & platforms
Visual FoxPro

RSA Sign Binary Data and Verify (Recover the Original Data)

See more RSA Examples

Demonstrates how to RSA sign binary data and then verify/recover the original data.

Note: This example uses methods introduced in Chilkat v9.5.0.77.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPrivKey
LOCAL loRsa
LOCAL lcOriginalData
LOCAL loBd
LOCAL loPubKey
LOCAL loRsa2
LOCAL lnBVerified

lnSuccess = 0

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

* Load an RSA private key for signing.
loPrivKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loPrivKey.LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem","passwd")
IF (lnSuccess = 0) THEN
    ? loPrivKey.LastErrorText
    RELEASE loPrivKey
    CANCEL
ENDIF

loRsa = CreateObject('Chilkat.Rsa')
loRsa.UsePrivateKey(loPrivKey)

* We have some binary data (in hex) to sign
lcOriginalData = "0102030405060708090A"
loBd = CreateObject('Chilkat.BinData')
loBd.AppendEncoded(lcOriginalData,"hex")

* If successful, the contents of bd are replaced with the RSA signature.
lnSuccess = loRsa.SignRawBd(loBd)
IF (lnSuccess = 0) THEN
    ? loRsa.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loBd
    CANCEL
ENDIF

* Show the RSA signature in base64
? loBd.GetEncoded("base64")

* ------------------------------------------
* Get the public key from the private key
loPubKey = CreateObject('Chilkat.PublicKey')
loPrivKey.ToPublicKey(loPubKey)

* Verify the signature and extract the original data.
loRsa2 = CreateObject('Chilkat.Rsa')
loRsa2.UsePublicKey(loPubKey)

lnBVerified = loRsa2.VerifyRawBd(loBd)
? "signature verified: " + STR(lnBVerified)

* Show the original data:
? "original data: " + loBd.GetEncoded("hex")

RELEASE loPrivKey
RELEASE loRsa
RELEASE loBd
RELEASE loPubKey
RELEASE loRsa2