If you want to decrypt a received email, keep in mind that you need the full encrypted message including the mime header.
<?php
// Get the full message
$encrypted = imap_fetchmime($stream, $msg_number, "1", FT_UID);
$encrypted .= imap_fetchbody($stream, $msg_number, "1", FT_UID);
// Write the needed temporary files
$infile = tempnam("", "enc");
file_put_contents($infile, $encrypted);
$outfile = tempnam("", "dec");
// The certification stuff
$public = file_get_contents("/path/to/your/cert.pem");
$private = array(file_get_contents("/path/to/your/cert.pem"), "password");
// Ready? Go!
if(openssl_pkcs7_decrypt($infile, $outfile, $public, $private))
{
// Decryption successful
echo file_get_contents($outfile);
}
else
{
// Decryption failed
echo "Oh oh! Decryption failed!";
}
// Remove the temp files
@unlink($infile);
@unlink($outfile);
?>
openssl_pkcs7_decrypt
(PHP 4 >= 4.0.6, PHP 5)
openssl_pkcs7_decrypt — Entschlüsseln einer S/MIME verschlüsselten Nachricht
Beschreibung
bool openssl_pkcs7_decrypt
( string
$infilename
, string $outfilename
, mixed $recipcert
[, mixed $recipkey
] )
Die Funktion openssl_pkcs7_decrypt() entschlüsselt
die mit S/MIME verschlüsselte Nachricht, die sich in der Datei befindet,
die mit dem Paramter infilename angegeben wurde.
Benutzt wird dafür das Zertifikat, angegeben durch
recipcert, und der damit verknüpfte private
Schlüssel, angegeben durch recipkey.
Parameter-Liste
-
infilename -
-
outfilename -
Die entschlüsselte Nachricht wird in die Datei namens
outfilenamegeschrieben. -
recipcert -
-
recipkey -
Rückgabewerte
Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.
Beispiele
Beispiel #1 openssl_pkcs7_decrypt() Beispiel
<?php
// Annahme: $cert enthält Ihr persönliches Zertifikat und
// $key Ihr privates Schlüsselpaar. Sie erhalten eine S/MIME Nachricht.
$infilename = "encrypted.msg"; // diese Datei enthält ihre verschlüsselte Nachricht
$outfilename = "decrypted.msg"; // stellen Sie sicher, dass Sie Schreibrechte haben!
if (openssl_pkcs7_decrypt($infilename, $outfilename, $cert, $key)) {
echo "entschlüsselt!";
} else {
echo "entschlüsseln fehlgeschlagen!";
}
?>
oliver at anonsphere dot com ¶
2 years ago
