Sample code for 30+ languages & platforms
Visual FoxPro

Generate an RSA Key and Save to Encrypted PEM

See more RSA Examples

Demonstrates how to generate an RSA key and save to an encrypted PEM file.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loRsa
LOCAL loPrivKey
LOCAL lcPassword
LOCAL lcPath
LOCAL loPubKey
LOCAL lnPreferPkcs1

lnSuccess = 0

loRsa = CreateObject('Chilkat.Rsa')

* Generate a 2048-bit key.
loPrivKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loRsa.GenKey(2048,loPrivKey)
IF (lnSuccess = 0) THEN
    ? loRsa.LastErrorText
    RELEASE loRsa
    RELEASE loPrivKey
    CANCEL
ENDIF

lcPassword = "secret"
* Saving to a relative path (from the current working directory of the process).
lcPath = "rsaKeys/myTestRsaPrivate.pem"
* Encrypt the PEM using 256-bit AES encryption.
loPrivKey.Pkcs8EncryptAlg = "aes256"
lnSuccess = loPrivKey.SavePkcs8EncryptedPemFile(lcPassword,lcPath)
IF (lnSuccess = 0) THEN
    ? loPrivKey.LastErrorText
    RELEASE loRsa
    RELEASE loPrivKey
    CANCEL
ENDIF

* image

* We can also save the public key.
* There is no need to encrypt public keys.
loPubKey = CreateObject('Chilkat.PublicKey')
loPrivKey.ToPublicKey(loPubKey)

lcPath = "rsaKeys/myTestRsaPublic.pem"
* Choose PKCS1 or PKCS8
* We'll choose PKCS8.
lnPreferPkcs1 = 0
lnSuccess = loPubKey.SavePemFile(lnPreferPkcs1,lcPath)
IF (lnSuccess = 0) THEN
    ? loPubKey.LastErrorText
    RELEASE loRsa
    RELEASE loPrivKey
    RELEASE loPubKey
    CANCEL
ENDIF

* image

? "Success."

RELEASE loRsa
RELEASE loPrivKey
RELEASE loPubKey