Sample code for 30+ languages & platforms
PowerBuilder

Duplicate OpensSSL to Create Signature using Cert and Key Files

See more OpenSSL Examples

This example duplicates the following:
openssl smime –sign -in something.xml -out something.der -signer mycert.crt -inkey cert.key -outform der –nodetach

Note: Although "smime" is the OpenSSL command, it's not actually producing S/MIME. The arguments "-outform der -binary" indicates that the output is binary DER (i.e. the PKCS7 binary signature). The input can be any type of file: XML, PDF, JPG, ... *anything*...

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
oleobject loo_Cert
oleobject loo_Bd
oleobject loo_Privkey

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

// Load the cert and private key from separate files.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("myCert.crt")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Crypt
    destroy loo_Cert
    return
end if

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Bd.LoadFile("cert.key")
loo_Privkey = create oleobject
li_rc = loo_Privkey.ConnectToNewObject("Chilkat.PrivateKey")

// Load from any format private key.
// If the file does not need a password, the 2nd arg is ignored.
li_Success = loo_Privkey.LoadAnyFormat(loo_Bd,"password_if_needed")
if li_Success <> 1 then
    Write-Debug loo_Privkey.LastErrorText
    destroy loo_Crypt
    destroy loo_Cert
    destroy loo_Bd
    destroy loo_Privkey
    return
end if

li_Success = loo_Crypt.SetSigningCert2(loo_Cert,loo_Privkey)
if li_Success <> 1 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Crypt
    destroy loo_Cert
    destroy loo_Bd
    destroy loo_Privkey
    return
end if

// Create the opaque signature (PKCS7 binary DER that contains both the signature and original file data).
li_Success = loo_Crypt.CreateP7M("qa_data/infile.anything","qa_output/outfile.der")
if li_Success <> 1 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Crypt
    destroy loo_Cert
    destroy loo_Bd
    destroy loo_Privkey
    return
end if

Write-Debug "Success."


destroy loo_Crypt
destroy loo_Cert
destroy loo_Bd
destroy loo_Privkey