Sample code for 30+ languages & platforms
Visual FoxPro

RSA Signature/Verify with .key and .cer

See more RSA Examples

Demonstrates how to use a .key file (private key) and digital certificate (.cer, public key) to create and verify an RSA signature.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPrivKey
LOCAL loRsa
LOCAL lcStrData
LOCAL lcHexSig
LOCAL loCert
LOCAL loPubKey
LOCAL loRsa2

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 .key file:
lnSuccess = loPrivKey.LoadPemFile("privateKey.key")
IF (lnSuccess = 0) THEN
    ? loPrivKey.LastErrorText
    RELEASE loPrivKey
    CANCEL
ENDIF

loRsa = CreateObject('Chilkat.Rsa')

* Import the private key into the RSA component:
lnSuccess = loRsa.UsePrivateKey(loPrivKey)
IF (lnSuccess = 0) THEN
    ? loRsa.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    CANCEL
ENDIF

* Create the signature as a hex string:
loRsa.EncodingMode = "hex"

lcStrData = "This is the string to be signed."

* Sign the string using the sha256 hash algorithm.
* Other valid choices are "md2", "sha1", "sha384",
* "sha512", and "md5".
lcHexSig = loRsa.SignStringENC(lcStrData,"sha256")

? lcHexSig

* Load a digital certificate from a .cer file:
loCert = CreateObject('Chilkat.Cert')

lnSuccess = loCert.LoadFromFile("myCert.cer")
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loCert
    CANCEL
ENDIF

loPubKey = CreateObject('Chilkat.PublicKey')
loCert.GetPublicKey(loPubKey)

* Now verify using a new instance of the RSA object:
loRsa2 = CreateObject('Chilkat.Rsa')

* Import the public key into the RSA object:
lnSuccess = loRsa2.UsePublicKey(loPubKey)
IF (lnSuccess = 0) THEN
    ? loRsa2.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loCert
    RELEASE loPubKey
    RELEASE loRsa2
    CANCEL
ENDIF

* The signature is a hex string, so make sure the EncodingMode is correct:
loRsa2.EncodingMode = "hex"

* Verify the signature:
lnSuccess = loRsa2.VerifyStringENC(lcStrData,"sha256",lcHexSig)
IF (lnSuccess = 0) THEN
    ? loRsa2.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loCert
    RELEASE loPubKey
    RELEASE loRsa2
    CANCEL
ENDIF

? "Success."

RELEASE loPrivKey
RELEASE loRsa
RELEASE loCert
RELEASE loPubKey
RELEASE loRsa2