openssl_cms_encrypt

(PHP 8)

openssl_cms_encryptCMS メッセージを暗号化する

説明

openssl_cms_encrypt(
    string $input_filename,
    string $output_filename,
    OpenSSLCertificate|array|string $certificate,
    ?array $headers,
    int $flags = 0,
    int $encoding = OPENSSL_ENCODING_SMIME,
    int $cipher_algo = OPENSSL_CIPHER_AES_128_CBC
): bool

この関数は、一人以上のメッセージの受け手に向けて、 それぞれの受け手の証明書を使ってメッセージを暗号化します。

パラメータ

input_filename

暗号化するファイル。

output_filename

ファイルの出力先。

certificate

暗号化されたメッセージの受け手の証明書。

headers

S/MIME を使う時に含めるヘッダ。

flags

CMS_sign に渡すフラグ。

encoding

出力のエンコーディング。 OPENSSL_ENCODING_SMIME, OPENSSL_ENCODING_DER, OPENSSL_ENCODING_PEM のいずれかです。

cipher_algo

使用する暗号化アルゴリズム。

戻り値

成功した場合に true を、失敗した場合に false を返します。

変更履歴

バージョン 説明
8.1.0 デフォルトの暗号化アルゴリズム (cipher_algo) が、 AES-128-CBC (OPENSSL_CIPHER_AES_128_CBC) になりました。 これより前のバージョンでは、 PKCS7/CMS (OPENSSL_CIPHER_RC2_40) が使われていました。
add a note

User Contributed Notes 1 note

up
7
Sebastian
4 years ago
It took me a while to find out the correct way how to sign and encrypt data with these functions.I needed that to communicate with German Health Insurance Providers as part of a DiGA. Maybe someone finds that useful.<?phpfunction signAndEncrypt(string $rawData): string{    $tempDir = __DIR__ . '/tmp';    $tempfileOriginal = tempnam($tempDir, 'original');    $tempfileSigned = tempnam($tempDir, 'signed');    $tempfileEncrypted = tempnam($tempDir, 'signedEncrypted');    file_put_contents($tempfileOriginal, $rawData);    // pick the correct certificate for the recipient    $recipientsCertificateFile = __DIR__ . '/recipientsCertificate.pem';    // -----BEGIN CERTIFICATE----- ...-----END CERTIFICATE-----    $recipientsCertificate = file_get_contents($recipientsCertificateFile);    // 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_sign(        input_filename: $tempfileOriginal,        output_filename: $tempfileSigned,        certificate: $myCertificate,        private_key: $myPrivateKey,        headers: [],        encoding: OPENSSL_ENCODING_DER,    );    openssl_cms_encrypt(        input_filename: $tempfileSigned,        output_filename: $tempfileEncrypted,        certificate: $recipientsCertificate,        headers: [],        flags: OPENSSL_CMS_BINARY | OPENSSL_CMS_NOSIGS | OPENSSL_CMS_NOVERIFY,        encoding: OPENSSL_ENCODING_DER,        cipher_algo: OPENSSL_CIPHER_AES_256_CBC    );    return file_get_contents($tempfileEncrypted);}
To Top