Sample code for 30+ languages & platforms
PowerBuilder

Create PKCS7 Signed File (.p7m)

See more Encryption Examples

Demonstrates how to sign a file to create a .p7m that contains both the file contents and the signature.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
oleobject loo_CertStore
oleobject loo_JsonCN
oleobject loo_Cert
string ls_InFile
string ls_OutFile

li_Success = 0

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

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_CertStore = create oleobject
li_rc = loo_CertStore.ConnectToNewObject("Chilkat.CertStore")

// Load a PFX file into a certificate store object.
li_Success = loo_CertStore.LoadPfxFile("myPfx.pfx","pfxPassword")
if li_Success <> 1 then
    Write-Debug loo_CertStore.LastErrorText
    destroy loo_Crypt
    destroy loo_CertStore
    return
end if

// Get the certificate by subject common name.
// This should be the cert within the PFX that also
// has a private key (also stored within the PFX).
loo_JsonCN = create oleobject
li_rc = loo_JsonCN.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonCN.UpdateString("CN","myCert")
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_Crypt
    destroy loo_CertStore
    destroy loo_JsonCN
    destroy loo_Cert
    return
end if

// Tell the crypt object to use the certificate for signing:
li_Success = loo_Crypt.SetSigningCert(loo_Cert)

// Sign a file, producing a .p7m as output.
// The input file is unchanged, the test.p7m contains the 
// contents of the input file and the signature.
ls_InFile = "test.txt"
ls_OutFile = "testp7m"
li_Success = loo_Crypt.CreateP7M(ls_InFile,ls_OutFile)
if li_Success <> 1 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Crypt
    destroy loo_CertStore
    destroy loo_JsonCN
    destroy loo_Cert
    return
end if

Write-Debug "Success!"


destroy loo_Crypt
destroy loo_CertStore
destroy loo_JsonCN
destroy loo_Cert