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

RSA Signature with Certificate's Private Key from PFX

See more RSA Examples

Demonstrates how to use a certificate's private key from a PFX file to create an RSA signature.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCertStore
LOCAL oJsonCN
LOCAL oCert
LOCAL oPrivKey
LOCAL oRsa
LOCAL cStrData
LOCAL cHexSig

nSuccess := 0

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

//  Create an instance of a certificate store object, load a PFX file,
//  locate the certificate we need, and use it for signing.
//  (a PFX file may contain more than one certificate.)
oCertStore := CreateObject("Chilkat.CertStore")

//  The 1st argument is the filename, the 2nd arg is the 
//  PFX file's password:
nSuccess := oCertStore:LoadPfxFile("chilkat.pfx", "test")
IF (nSuccess == 0)
    ? oCertStore:LastErrorText
    oCertStore:destroy()
    RETURN
ENDIF

//  Find the certificate by the subject common name:
oJsonCN := CreateObject("Chilkat.JsonObject")
oJsonCN:UpdateString("CN", "cert common name")

oCert := CreateObject("Chilkat.Cert")
nSuccess := oCertStore:FindCert(oJsonCN, oCert)
IF (nSuccess == 0)
    ? oCertStore:LastErrorText
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oCert:GetPrivateKey(oPrivKey)
IF (nSuccess == 0)
    ? oCert:LastErrorText
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

oRsa := CreateObject("Chilkat.Rsa")

nSuccess := oRsa:UsePrivateKey(oPrivKey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    oPrivKey:destroy()
    oRsa:destroy()
    RETURN
ENDIF

//  Encode the signature as a hex string
oRsa:EncodingMode := "hex"

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

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

? cHexSig

? "Success!"

oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oPrivKey:destroy()
oRsa:destroy()