CkPhp Requires Chilkat v11.8.0+
CkPhp
Add PEM Text as a Decryption Source
See more MIME Examples
Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.
PEM text held in memory is loaded into a Pem object with LoadPem, and AddPemSource registers its certificates and private keys as a source for later decryption.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background. This is the in-memory counterpart of loading a PEM file with LoadPemFile, for cases where the PEM text is already in memory rather than residing in a file.
AddPemSource was added in Chilkat v11.8.0 and replaces UseCertVault and the AddPfxSource methods.Chilkat CkPhp Downloads
<?php
include("chilkat.php");
$success = false;
$mime = new CkMime();
$success = $mime->LoadMimeFile('qa_data/encrypted.eml');
if ($success == false) {
print $mime->lastErrorText() . "\n";
exit;
}
// PEM text (a certificate and its private key) that is held in memory. Here it is loaded into a
// StringBuilder from a file.
$sbPem = new CkStringBuilder();
$success = $sbPem->LoadFile('qa_data/recipient.pem','utf-8');
if ($success == false) {
print $sbPem->lastErrorText() . "\n";
exit;
}
// Load the PEM text into a Pem object. The 2nd argument of LoadPem is the password of the private
// key (from a secure source).
$pemPassword = 'myPemPassword';
$pem = new CkPem();
$success = $pem->LoadPem($sbPem->getAsString(),$pemPassword);
if ($success == false) {
print $pem->lastErrorText() . "\n";
exit;
}
// Add the Pem object as a source of certificates and private keys for later decryption.
$success = $mime->AddPemSource($pem);
if ($success == false) {
print $mime->lastErrorText() . "\n";
exit;
}
$success = $mime->Decrypt();
if ($success == false) {
print $mime->lastErrorText() . "\n";
exit;
}
print 'Decrypted.' . "\n";
?>