Sample code for 30+ languages & platforms
Xbase++

CAdES BES Detached Signature

See more Encryption Examples

Demonstrates how to create a CAdES BES detached signature file (.p7s).

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCrypt
LOCAL cPfxPath
LOCAL cPfxPassword
LOCAL oCert
LOCAL cInFile
LOCAL cSigFile

nSuccess := 0

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

oCrypt := CreateObject("Chilkat.Crypt2")

//  Use a digital certificate and private key from a PFX file (.pfx or .p12).
cPfxPath := "/Users/chilkat/testData/pfx/acme.pfx"
cPfxPassword := "test123"

oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxFile(cPfxPath, cPfxPassword)
IF (nSuccess != 1)
    ? oCert:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Tell the crypt component to use this cert.
nSuccess := oCrypt:SetSigningCert(oCert)
IF (nSuccess != 1)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  The CadesEnabled property applies to all methods that create PKCS7 signatures. 
//  To create a CAdES-BES signature, set this property equal to true. 
oCrypt:CadesEnabled := 1

//  We can sign any type of file, creating a .p7s as output:
cInFile := "/Users/chilkat/testData/pdf/sample.pdf"
cSigFile := "/Users/chilkat/testData/p7s/sample.p7s"

//  Create the detached CAdES-BES signature:
nSuccess := oCrypt:CreateP7S(cInFile, cSigFile)
IF (nSuccess == 0)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    RETURN
ENDIF

nSuccess := oCrypt:VerifyP7S(cInFile, cSigFile)
IF (nSuccess == 0)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCert:destroy()
    RETURN
ENDIF

? "Success!"

oCrypt:destroy()
oCert:destroy()