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

RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature

See more RSA Examples

Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRsa
LOCAL oPfx
LOCAL oPrivKey
LOCAL cStrData
LOCAL cBase64Sig

nSuccess := 0

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

oRsa := CreateObject("Chilkat.Rsa")

//  Load the .pfx/.p12
oPfx := CreateObject("Chilkat.Pfx")
nSuccess := oPfx:LoadPfxFile("qa_data/pfx/myKey.p12", "myPassword")
IF (nSuccess == 0)
    ? oPfx:LastErrorText
    oRsa:destroy()
    oPfx:destroy()
    RETURN
ENDIF

//  Get the default private key.

oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPfx:PrivateKeyAt(0, oPrivKey)
IF (nSuccess == 0)
    ? oPfx:LastErrorText
    oRsa:destroy()
    oPfx:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

//  Import the private key into the RSA component:
nSuccess := oRsa:UsePrivateKey(oPrivKey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oRsa:destroy()
    oPfx:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

//  Get the signature in base64
oRsa:EncodingMode := "base64"

cStrData := "This is the string to be signed."

//  Sign the string using the sha256 hash algorithm.
//  Other valid choices are "sha384", "sha512", "sha-1", "md2" and "md5".
cBase64Sig := oRsa:SignStringENC(cStrData, "sha256")

? cBase64Sig

? "Success!"

oRsa:destroy()
oPfx:destroy()
oPrivKey:destroy()