Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCrypt
LOCAL oCert
LOCAL oBd
LOCAL oPrivkey

nSuccess := 0

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

oCrypt := CreateObject("Chilkat.Crypt2")

//  Load the cert and private key from separate files.
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromFile("myCert.crt")
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    RETURN
ENDIF

oBd := CreateObject("Chilkat.BinData")
nSuccess := oBd:LoadFile("cert.key")
oPrivkey := CreateObject("Chilkat.PrivateKey")
//  Load from any format private key.
//  If the file does not need a password, the 2nd arg is ignored.
nSuccess := oPrivkey:LoadAnyFormat(oBd, "password_if_needed")
IF (nSuccess != 1)
    ? oPrivkey:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    oBd:destroy()
    oPrivkey:destroy()
    RETURN
ENDIF

nSuccess := oCrypt:SetSigningCert2(oCert, oPrivkey)
IF (nSuccess != 1)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    oBd:destroy()
    oPrivkey:destroy()
    RETURN
ENDIF

//  Create the opaque signature (PKCS7 binary DER that contains both the signature and original file data).
nSuccess := oCrypt:CreateP7M("qa_data/infile.anything", "qa_output/outfile.der")
IF (nSuccess != 1)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    oBd:destroy()
    oPrivkey:destroy()
    RETURN
ENDIF

? "Success."

oCrypt:destroy()
oCert:destroy()
oBd:destroy()
oPrivkey:destroy()