Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

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

loCrypt = createobject("CkCrypt2")

loCertStore = createobject("CkCertStore")

// Load a PFX file into a certificate store object.
llSuccess = loCertStore.LoadPfxFile("myPfx.pfx","pfxPassword")
if (llSuccess <> .T.) then
    ? loCertStore.LastErrorText
    release loCrypt
    release loCertStore
    return
endif

// 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).
loJsonCN = createobject("CkJsonObject")
loJsonCN.UpdateString("CN","myCert")
loCert = createobject("CkCert")
llSuccess = loCertStore.FindCert(loJsonCN,loCert)
if (llSuccess = .F.) then
    ? loCertStore.LastErrorText
    release loCrypt
    release loCertStore
    release loJsonCN
    release loCert
    return
endif

// Tell the crypt object to use the certificate for signing:
llSuccess = loCrypt.SetSigningCert(loCert)

// 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.
lcInFile = "test.txt"
lcOutFile = "testp7m"
llSuccess = loCrypt.CreateP7M(lcInFile,lcOutFile)
if (llSuccess <> .T.) then
    ? loCrypt.LastErrorText
    release loCrypt
    release loCertStore
    release loJsonCN
    release loCert
    return
endif

? "Success!"


release loCrypt
release loCertStore
release loJsonCN
release loCert