<HTML ><HEAD ><TITLE >openssl_pkcs7_encrypt</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="OpenSSL functions" HREF="ref.openssl.html"><LINK REL="PREVIOUS" TITLE="openssl_pkcs7_decrypt" HREF="function.openssl-pkcs7-decrypt.html"><LINK REL="NEXT" TITLE="openssl_pkcs7_sign" HREF="function.openssl-pkcs7-sign.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.openssl-pkcs7-decrypt.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.openssl-pkcs7-sign.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.openssl-pkcs7-encrypt" ></A >openssl_pkcs7_encrypt</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN68248" ></A ><P > (PHP 4 >= 4.0.6)</P >openssl_pkcs7_encrypt -- Encrypt an S/MIME message</DIV ><DIV CLASS="refsect1" ><A NAME="AEN68251" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >openssl_pkcs7_encrypt</B > ( string infile, string outfile, mixed recipcerts, array headers [, long flags])<BR ></BR ><P > <B CLASS="function" >openssl_pkcs7_encrypt()</B > takes the contents of the file named <TT CLASS="parameter" ><I >infile</I ></TT > and encrypts them using an RC2 40-bit cipher so that they can only be read by the intended recipients specified by <TT CLASS="parameter" ><I >recipcerts</I ></TT >, which is either a lone X.509 certificate, or an array of X.509 certificates. <TT CLASS="parameter" ><I >headers</I ></TT > is an array of headers that will be prepended to the data after it has been encrypted. <TT CLASS="parameter" ><I >flags</I ></TT > can be used to specify options that affect the encoding process - see <A HREF="ref.openssl.html#openssl.pkcs7.flags" >PKCS7 constants</A >. <TT CLASS="parameter" ><I >headers</I ></TT > can be either an associative array keyed by header name, or an indexed array, where each element contains a single header line. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN68280" ></A ><P ><B >Example 1. <B CLASS="function" >openssl_pkcs7_encrypt()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// the message you want to encrypt and send to your secret agent // in the field, known as nighthawk. You have his certificate // in the file nighthawk.pem $data = <<<EOD Nighthawk, Top secret, for your eyes only! The enemy is closing in! Meet me at the cafe at 8.30am to collect your forged passport! HQ EOD; // load key $key = file_get_contents("nighthawk.pem"); // save message to file $fp = fopen("msg.txt", "w"); fwrite($fp, $data); fclose($fp); // encrypt it if (openssl_pkcs7_encrypt("msg.txt", "enc.txt", $key, array("To" => "nighthawk@example.com", // keyed syntax "From: HQ <hq@example.com>", // indexed syntax "Subject" => "Eyes only"))) { // message encrypted - send it! exec(ini_get("sendmail_path") . " < enc.txt"); }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.openssl-pkcs7-decrypt.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.openssl-pkcs7-sign.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >openssl_pkcs7_decrypt</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.openssl.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >openssl_pkcs7_sign</TD ></TR ></TABLE ></DIV ></BODY ></HTML >