# Encrypt

This filter will encrypt any given string with the provided setting. Therefor it makes use of Adapters. Actually there are adapters for the `Mcrypt` and `OpenSSL` extensions from php.

As these two encryption methodologies work completely different, also the usage of the adapters differ. You have to select the adapter you want to use when initiating the filter.

    // Use the Mcrypt adapter
    $filter1 = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt'));

    // Use the OpenSSL adapter
    $filter2 = new Zend_Filter_Encrypt(array('adapter' => 'openssl'));

To set another adapter you can also use `setAdapter()`, and the `getAdapter()` method to receive the actual set adapter.

    // Use the Mcrypt adapter
    $filter = new Zend_Filter_Encrypt();
    $filter->setAdapter('openssl');

When you do not supply the `adapter` option or do not use setAdapter, then the `Mcrypt` adapter will be used per default.

## Encryption with Mcrypt

When you have installed the `Mcrypt` extension you can use the `Mcrypt` adapter. This adapter supports the following options at initiation:

- key: The encryption key with which the input will be encrypted. You need the same key for decryption.

- algorithm: The algorithm which has to be used. It should be one of the algorithm ciphers which can be found under PHP's mcrypt ciphers. If not set it defaults to `blowfish`.

- algorithm_directory: The directory where the algorithm can be found. If not set it defaults to the path set within the mcrypt extension.

- mode: The encryption mode which has to be used. It should be one of the modes which can be found under PHP's mcrypt modes. If not set it defaults to `cbc`.

- mode_directory: The directory where the mode can be found. If not set it defaults to the path set within the `mcrypt` extension.

- vector: The initialization vector which shall be used. If not set it will be a random vector.

- salt: If the key should be used as salt value. The key used for encryption will then itself also be encrypted. Default is false.

If you give a string instead of an array, this string will be used as key.

You can get/set the encryption values also afterwards with the `getEncryption()` and `setEncryption()` methods.

Note that you will get an exception if the mcrypt extension is not available in your environment.

You should also note that all settings which be checked when you create the instance or when you call setEncryption(). If mcrypt detects problem with your settings an exception will be thrown.

You can get/set the encryption vector by calling `getVector()` and `setVector()`. A given string will be truncated or padded to the needed vector size of the used algorithm.

Note that when you are not using an own vector, you must get the vector and store it. Otherwise you will not be able to decode the encoded string.

    // Use the default blowfish settings
    $filter = new Zend_Filter_Encrypt('myencryptionkey');

    // Set a own vector, otherwise you must call getVector()
    // and store this vector for later decryption
    $filter->setVector('myvector');
    // $filter->getVector();

    $encrypted = $filter->filter('text_to_be_encoded');
    print $encrypted;

    // For decryption look at the Decrypt filter

## Encryption with OpenSSL

When you have installed the `OpenSSL` extension you can use the `OpenSSL` adapter. This adapter supports the following options at initiation:

- public: The public key of the user whom you want to provide the encrpted content. You can give multiple public keys by using an array. You can eigther provide the path and filename of the key file, or just the content of the key file itself.

- private: Your private key which will be used for encrypting the content. Also the private key can be either a filename with path of the key file, or just the content of the key file itself.

You can get/set the public keys also afterwards with the `getPublicKey()` and `setPublicKey()` methods. The private key can also be get and set with the related `getPrivateKey()` and `setPrivateKey()` methods.

    // Use openssl and provide a private key
    $filter = new Zend_Filter_Encrypt(array(
        'adapter' => 'openssl',
        'private' => '/path/to/mykey/private.pem'
    ));

    // of course you can also give the public keys at initiation
    $filter->setPublicKey(array(
        '/public/key/path/first.pem',
        '/public/key/path/second.pem'
    ));

Note that the `OpenSSL` adapter will not work when you do not provide valid keys.

When you want to encode also the keys, then you have to provide a passphrase with the `setPassphrase()` method. When you want to decode content which was encoded with a passphrase you will not only need the public key, but also the passphrase to decode the encrypted key.

    // Use openssl and provide a private key
    $filter = new Zend_Filter_Encrypt(array(
        'adapter' => 'openssl',
        'private' => '/path/to/mykey/private.pem'
    ));

    // of course you can also give the public keys at initiation
    $filter->setPublicKey(array(
        '/public/key/path/first.pem',
        '/public/key/path/second.pem'
    ));
    $filter->setPassphrase('mypassphrase');

At last, when you use OpenSSL you need to give the receiver the encrypted content, the passphrase when have provided one, and the envelope keys for decryption.

This means for you, that you have to get the envelope keys after the encryption with the `getEnvelopeKey()` method.

So our complete example for encrypting content with `OpenSSL` look like this.

    // Use openssl and provide a private key
    $filter = new Zend_Filter_Encrypt(array(
        'adapter' => 'openssl',
        'private' => '/path/to/mykey/private.pem'
    ));

    // of course you can also give the public keys at initiation
    $filter->setPublicKey(array(
        '/public/key/path/first.pem',
        '/public/key/path/second.pem'
    ));
    $filter->setPassphrase('mypassphrase');

    $encrypted = $filter->filter('text_to_be_encoded');
    $envelope  = $filter->getEnvelopeKey();
    print $encrypted;

    // For decryption look at the Decrypt filter