LonghornPHP 2026

openssl_cms_decrypt

(PHP 8)

openssl_cms_decryptРасшифровывает CMS-сообщение

Описание

function openssl_cms_decrypt(
string $input_filename,
string $output_filename,
#[\SensitiveParameter]OpenSSLCertificate |string $certificate,
#[\SensitiveParameter]OpenSSLAsymmetricKey |OpenSSLCertificate |array |string |null $private_key = null ,
int $encoding = OPENSSL_ENCODING_SMIME
): bool

Расшифровывает CMS-сообщение.

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

input_filename

Имя файла, который содержит зашифрованное содержимое.

output_filename

Имя файла для хранения расшифрованного содержимого.

certificate

Имя файла, содержащего сертификат получателя.

private_key

Имя файла, содержащего ключ PKCS#8.

encoding

Кодировка входного файла: OPENSSL_ENCODING_SMIME , OPENSSL_ENCODING_DER или OPENSSL_ENCODING_PEM .

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

Функция возвращает true , если выполнилась успешно, или false , если возникла ошибка.

Нашли ошибку?

ИнструкцияИсправлениеСообщение об ошибке
+Добавить

Примечания пользователей 1 note

up
3
Sebastian
5 years ago
It took me a while to find out the correct way how to decrypt and verify data with these functions.
I needed that to communicate with German Health Insurance Providers as part of a DiGA. Maybe someone finds that useful.
<?php
function decryptAndVerify($signedAndEncryptedRawData): string
{
 $tempDir = __DIR__ . '/tmp';
 $originalFile = tempnam($tempDir, 'original');
 $decryptedFile = tempnam($tempDir, 'decrypted');
 $verifiedFile = tempnam($tempDir, 'verified');
 file_put_contents($originalFile, $signedAndEncryptedRawData);
 
 // One file with all possible certificates one after the other
 // -----BEGIN CERTIFICATE----- ...-----END CERTIFICATE-----
 $allPossibleSenderCertificates = __DIR__ . '/untrusted.pem';
 // Certificate:
 // Data:
 // Version: 3 (0x2)...
 $myCertificate = file_get_contents(__DIR__ . '/my.crt');
 $myPrivateKey = openssl_pkey_get_private(
 // -----BEGIN RSA PRIVATE KEY----- ... -----END RSA PRIVATE KEY-----
 file_get_contents(__DIR__ . '/my.prv.key.pem')
 );
 
 openssl_cms_decrypt(
 input_filename: $originalFile,
 output_filename: $decryptedFile,
 certificate: $myCertificate,
 private_key: $myPrivateKey,
 encoding: OPENSSL_ENCODING_DER
 );
 openssl_cms_verify(
 input_filename: $decryptedFile,
 flags: OPENSSL_CMS_BINARY | OPENSSL_CMS_NOSIGS | OPENSSL_CMS_NOVERIFY,
 ca_info: [],
 untrusted_certificates_filename: $allPossibleSenderCertificates,
 content: $verifiedFile,
 encoding: OPENSSL_ENCODING_DER
 );
 return file_get_contents($verifiedFile);
}
+Добавить

AltStyle によって変換されたページ (->オリジナル) /