# Decrypt

This filter will decrypt 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.

For details about how to encrypt content look at the `Encrypt` filter. As the basics are covered within the `Encrypt` filter, we will describe here only the needed additional methods and changes for decryption.

## Decryption with Mcrypt

For decrypting content which was previously encrypted with `Mcrypt` you need to have the options with which the encryption has been called.

There is one emminent difference for you. When you did not provide a vector at encryption you need to get it after you encrypted the content by using the `getVector()` method on the encryption filter. Without the correct vector you will not be able to decrypt the content.

As soon as you have provided all options decryption is as simple as encryption.

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

    // Set the vector with which the content was encrypted
    $filter->setVector('myvector');

    $decrypted = $filter->filter('encoded_text_normally_unreadable');
    print $decrypted;

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.

## Decryption with OpenSSL

Decryption with `OpenSSL` is as simple as encryption. But you need to have all data from the person who encrypted the content.

For decryption with `OpenSSL` you need:

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

- envelope: The encrypted envelope key from the user who encrypted the content. You can eigther provide the path and filename of the key file, or just the content of the key file itself.

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

    // of course you can also give the envelope keys at initiation
    $filter->setEnvelopeKey(array(
        '/key/from/encoder/first.pem',
        '/key/from/encoder/second.pem'
    ));

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

Optionally it could be necessary to provide the passphrase for decrypting the keys themself by using the `setPassphrase()` method.

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

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

At last, decode the content. Our complete example for decrypting the previously encrypted content looks like this.

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

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

    $decrypted = $filter->filter('encoded_text_normally_unreadable');
    print $decrypted;