/** * Defined by IfwPsn_Vendor_Zend_Filter_Interface * * Encrypts $value with the defined settings * * @param string $value The content to encrypt * @return string The encrypted content */ public function encrypt($value) { // compress prior to encryption if (!empty($this->_compression)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Compress.php'; $compress = new IfwPsn_Vendor_Zend_Filter_Compress($this->_compression); $value = $compress->filter($value); } $cipher = $this->_openCipher(); $this->_initCipher($cipher); $encrypted = mcrypt_generic($cipher, $value); mcrypt_generic_deinit($cipher); $this->_closeCipher($cipher); return $encrypted; }
/** * Encrypts $value with the defined settings * Note that you also need the "encrypted" keys to be able to decrypt * * @param string $value Content to encrypt * @return string The encrypted content * @throws IfwPsn_Vendor_Zend_Filter_Exception */ public function encrypt($value) { $encrypted = array(); $encryptedkeys = array(); if (count($this->_keys['public']) == 0) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php'; throw new IfwPsn_Vendor_Zend_Filter_Exception('Openssl can not encrypt without public keys'); } $keys = array(); $fingerprints = array(); $count = -1; foreach ($this->_keys['public'] as $key => $cert) { $keys[$key] = openssl_pkey_get_public($cert); if ($this->_package) { $details = openssl_pkey_get_details($keys[$key]); if ($details === false) { $details = array('key' => 'ZendFramework'); } ++$count; $fingerprints[$count] = md5($details['key']); } } // compress prior to encryption if (!empty($this->_compression)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Compress.php'; $compress = new IfwPsn_Vendor_Zend_Filter_Compress($this->_compression); $value = $compress->filter($value); } $crypt = openssl_seal($value, $encrypted, $encryptedkeys, $keys); foreach ($keys as $key) { openssl_free_key($key); } if ($crypt === false) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php'; throw new IfwPsn_Vendor_Zend_Filter_Exception('Openssl was not able to encrypt your content with the given options'); } $this->_keys['envelope'] = $encryptedkeys; // Pack data and envelope keys into single string if ($this->_package) { $header = pack('n', count($this->_keys['envelope'])); foreach ($this->_keys['envelope'] as $key => $envKey) { $header .= pack('H32n', $fingerprints[$key], strlen($envKey)) . $envKey; } $encrypted = $header . $encrypted; } return $encrypted; }