Sample code for 30+ languages & platforms
Visual FoxPro

Generate an RSA Key and Get as Base64 DER

See more RSA Examples

Demonstrates how to generate a 2048-bit RSA key and return the public and private parts as unencrypted Base64 encoded DER.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loRsa
LOCAL loPrivKey
LOCAL loPubKey
LOCAL lnBChoosePkcs1
LOCAL lcPubKeyBase64Der
LOCAL lcPrivKeyPkcs1
LOCAL lcPrivKeyPkcs8

lnSuccess = 0

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

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

* Get the public part of the key.
loPubKey = CreateObject('Chilkat.PublicKey')
loPrivKey.ToPublicKey(loPubKey)

* There are two possible formats for representing the RSA public key 
* in ASN.1 (DER).  The possible formats are PKCS1 and PKCS8.
* We can get either by setting bChoosePkcs1 to 1 or 0.
lnBChoosePkcs1 = 1
lcPubKeyBase64Der = loPubKey.GetEncoded(lnBChoosePkcs1,"base64")
? "Public Key Base64 DER:"
? lcPubKeyBase64Der

* Get the private key as Base64 DER:
* We can get PKCS1 or PKCS8, but with different methods:
lcPrivKeyPkcs1 = loPrivKey.GetPkcs1ENC("base64")
? "Private Key PKCS1 Base64 DER:"
? lcPrivKeyPkcs1

lcPrivKeyPkcs8 = loPrivKey.GetPkcs8ENC("base64")
? "Private Key PKCS8 Base64 DER:"
? lcPrivKeyPkcs8

RELEASE loRsa
RELEASE loPrivKey
RELEASE loPubKey