Sample code for 30+ languages & platforms
Visual FoxPro

RSA Sign with PKCS8 Encrypted Key

See more RSA Examples

Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).

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 PEM file:
lnSuccess = loPrivKey.LoadAnyFormatFile("raul_privateKey.key","a0123456789")
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

* This example will sign a string, and receive the signature
* in a hex-encoded string.  Therefore, set the encoding mode
* to "hex":
loRsa.EncodingMode = "hex"

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

* Sign the string using the sha256 hash algorithm.
* Other valid choices are sha1, sha384, sha512 and others.
lcHexSig = loRsa.SignStringENC(lcStrData,"sha256")
IF (loRsa.LastMethodSuccess = 0) THEN
    ? loRsa.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    CANCEL
ENDIF

? lcHexSig

* Now verify with the public key.
* This example shows how to use the public key from 
* a digital certificate (.cer file)
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadFromFile("raul_publicKey.cer")
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loPrivKey
    RELEASE loRsa
    RELEASE loCert
    CANCEL
ENDIF

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

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

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

? "Signature verified!"

* Verify with incorrect data:
lnSuccess = loRsa2.VerifyStringENC("something else","sha256",lcHexSig)
IF (lnSuccess <> 1) THEN
    ? "Signature not verified! (which was expected in this case)"
ELSE
    ? "Hmmm... that's not right..."
ENDIF

RELEASE loPrivKey
RELEASE loRsa
RELEASE loCert
RELEASE loPubKey
RELEASE loRsa2