PHP 8.3.4 Released!

openssl_pkcs7_decrypt

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

openssl_pkcs7_decryptРасшифровать сообщение, зашифрованное S/MIME

Описание

openssl_pkcs7_decrypt(
    string $input_filename,
    string $output_filename,
    OpenSSLCertificate|string $certificate,
    OpenSSLAsymmetricKey|OpenSSLCertificate|array|string|null $private_key = null
): bool

Расшифровывает сообщение, зашифрованное S/MIME, содержащееся в файле input_filename, с использованием сертификата certificate и соответствующего закрытого ключа private_key.

Список параметров

input_filename

output_filename

Расшифрованное сообщение будет записано в файл output_filename.

certificate

private_key

Возвращаемые значения

Возвращает true в случае успешного выполнения или false в случае ошибки.

Список изменений

Версия Описание
8.0.0 private_key теперь принимает экземпляр OpenSSLAsymmetricKey или OpenSSLCertificate; ранее принимался ресурс (resource) типа OpenSSL key или OpenSSL X.509 CSR.

Примеры

Пример #1 Пример использования openssl_pkcs7_decrypt()

<?php
// $cert и $key содержат пару с личным сертификатом и закрытым ключом
$infilename = "encrypted.msg"; // в этом файле зашифрованное сообщение
$outfilename = "decrypted.msg"; // убедитесь, что у вас есть права на запись

if (openssl_pkcs7_decrypt($infilename, $outfilename, $cert, $key)) {
echo
"расшифровано!";
} else {
echo
"возникла ошибка при расшифровке!";
}
?>

add a note

User Contributed Notes 1 note

up
1
oliver at anonsphere dot com
12 years ago
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);

?>
To Top