Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Generate RSA Public/Private Key Pair and Export to PEM

See more RSA Examples

_LANGUAGE_ example code showing how to generate an RSA public/private key pair and export to PEM files.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRsa
LOCAL oPrivKey
LOCAL oPubKey

nSuccess := 0

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

oRsa := CreateObject("Chilkat.Rsa")

//  Generate a 2048-bit key.  Chilkat RSA supports
//  key sizes ranging from 512 bits to 8192 bits.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oRsa:GenKey(2048, oPrivKey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oRsa:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

oPubKey := CreateObject("Chilkat.PublicKey")
oPrivKey:ToPublicKey(oPubKey)

//  Save the private key in PEM format:
nSuccess := oPrivKey:SavePemFile("privateKey.pem")
IF (nSuccess == 0)
    ? oPrivKey:LastErrorText
    oRsa:destroy()
    oPrivKey:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

//  Save the public key in PEM format:
nSuccess := oPubKey:SavePemFile(0, "publicKey.pem")
IF (nSuccess == 0)
    ? oPubKey:LastErrorText
    oRsa:destroy()
    oPrivKey:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

? "Success."

oRsa:destroy()
oPrivKey:destroy()
oPubKey:destroy()