Sample code for 30+ languages & platforms
PHP Requires Chilkat v10.1.2+

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

PHP
<?php

use Chilkat2\Cert;
use Chilkat2\CertStore;
use Chilkat2\Crypt2;
use Chilkat2\JsonObject;

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

$crypt = new Crypt2();

$certStore = new CertStore();

// Load a PFX file into a certificate store object.
try {
    $certStore->LoadPfxFile('myPfx.pfx', 'pfxPassword');

    // 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).
    $jsonCN = new JsonObject();
    $jsonCN->UpdateString('CN', 'myCert');
    $cert = new Cert();
    $certStore->FindCert($jsonCN, $cert);

    // Tell the crypt object to use the certificate for signing:
    $crypt->SetSigningCert($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.
    $inFile = 'test.txt';
    $outFile = 'testp7m';
    $crypt->CreateP7M($inFile, $outFile);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "Success!\n";