Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_CertStore
oleobject loo_JsonCN
oleobject loo_Cert
oleobject loo_PrivKey
oleobject loo_Rsa
string ls_StrData
string ls_HexSig

li_Success = 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.)
loo_CertStore = create oleobject
li_rc = loo_CertStore.ConnectToNewObject("Chilkat.CertStore")
if li_rc < 0 then
    destroy loo_CertStore
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// The 1st argument is the filename, the 2nd arg is the 
// PFX file's password:
li_Success = loo_CertStore.LoadPfxFile("chilkat.pfx","test")
if li_Success = 0 then
    Write-Debug loo_CertStore.LastErrorText
    destroy loo_CertStore
    return
end if

// Find the certificate by the subject common name:
loo_JsonCN = create oleobject
li_rc = loo_JsonCN.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonCN.UpdateString("CN","cert common name")

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_CertStore.FindCert(loo_JsonCN,loo_Cert)
if li_Success = 0 then
    Write-Debug loo_CertStore.LastErrorText
    destroy loo_CertStore
    destroy loo_JsonCN
    destroy loo_Cert
    return
end if

loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_Cert.GetPrivateKey(loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_CertStore
    destroy loo_JsonCN
    destroy loo_Cert
    destroy loo_PrivKey
    return
end if

loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")

li_Success = loo_Rsa.UsePrivateKey(loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_CertStore
    destroy loo_JsonCN
    destroy loo_Cert
    destroy loo_PrivKey
    destroy loo_Rsa
    return
end if

// Encode the signature as a hex string
loo_Rsa.EncodingMode = "hex"

ls_StrData = "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".
ls_HexSig = loo_Rsa.SignStringENC(ls_StrData,"sha-1")

Write-Debug ls_HexSig

Write-Debug "Success!"


destroy loo_CertStore
destroy loo_JsonCN
destroy loo_Cert
destroy loo_PrivKey
destroy loo_Rsa