Crypt Component


    Phalcon provides encryption facilities via the Phalcon\Crypt component. This class offers simple object-oriented wrappers to the PHP’s encryption library.

    By default, this component utilizes the AES-256-CFB cipher.

    The cipher AES-256 is used among other places in SSL/TLS across the Internet. It’s considered among the top ciphers. In theory it’s not crackable since the combinations of keys are massive. Although NSA has categorized this in Suite B, they have also recommended using higher than 128-bit keys for encryption.

    Basic Usage

    This component is designed to be very simple to use:

    If no parameters are passed in the constructor, the component will use the aes-256-cfb cipher with signing by default. You can always change the cipher as well as disable signing.

    1. <?php
    2. use Phalcon\Crypt;
    3. $key = "12345"; // Your luggage combination
    4. $crypt = new Crypt();
    5. $crypt
    6. ->setCipher('aes-256-gcm')
    7. ->useSigning(false)
    8. ;
    9. $text = 'This is the text that you want to encrypt.';
    10. $encrypted = $crypt->encrypt($text, $key);
    11. echo $crypt->decrypt($encrypted, $key);

    Encrypt

    1. <?php
    2. use Phalcon\Crypt;
    3. $key = "12345"; // Your luggage combination
    4. $crypt = new Crypt();
    5. $crypt->setKey($key);
    6. $text = 'This is the text that you want to encrypt.';
    7. $encrypted = $crypt->encrypt($text);

    or using the key as the second parameter

    The method will also internally use signing by default. You can always use useSigning(false) prior to the method call to disable it.

    The decrypt() method decrypts a string. Similar to encrypt() the component will use the previously set cipher, which has been set in the constructor or explicitly. If no key is passed in the parameter, the previously set key will be used.

    1. use Phalcon\Crypt;
    2. $key = "12345"; // Your luggage combination
    3. $crypt = new Crypt();
    4. $crypt->setKey($key);
    5. $text = 'T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3';
    6. $encrypted = $crypt->decrypt($text);

    or using the key as the second parameter

    1. <?php
    2. $key = "12345"; // Your luggage combination
    3. $crypt = new Crypt();
    4. $crypt->setKey($key);
    5. $text = 'T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3';
    6. $encrypted = $crypt->decrypt($text, $key);

    The method will also internally use signing by default. You can always use useSigning(false) prior to the method call to disable it.

    Base64 Encrypt

    The encryptBase64() can be used to encrypt a string in a URL friendly way. It uses encrypt() internally and accepts the text and optionally the key of the element to encrypt. There is also a third parameter safe (defaults to false) which will perform string replacements for non URL friendly characters such as + or /.

    Base64 Decrypt

    The decryptBase64() can be used to deecrypt a string in a URL friendly way. Similar to encryptBase64() it uses decrypt() internally and accepts the text and optionally the key of the element to encrypt. There is also a third parameter safe (defaults to false) which will perform string replacements for previously replaced non URL friendly characters such as + or /.

    Exceptions thrown in the component will be of type [Phalcon\Crypt\Exception][config-exception]. If however you are using signing and the calculated hash for decrypt() does not match, Phalcon\Crypt\Mismatch will be thrown. You can use these exceptions to selectively catch exceptions thrown only from this component.

    Functionality

    The getter getCipher() returns the currently selected cipher. If none has been explicitly defined either by the setter setCipher() or the constructor of the object the aes-256-cfb is selected by default. The aes-256-gcm is the preferable cipher.

    Hash algorithm

    The getter getHashAlgo() returns the hashing algorithm use by the component. If none has been explicitly defined by the setter setHashAlgo() the sha256 will be used. If the hash algorithm defined is not available in the system or is wrong, a [Phalcon\Crypt\Exception][crypt=exception] will be thrown.

    You can always get an array of all the available hashing algorithms for your system by calling getAvailableHashAlgos().

    The component offers a getter and a setter for the key to be used. Once the key is set, it will be used for any encrypting or decrypting operation (provided that the key parameter is not defined when using these methods).

    • getKey(): Returns the encryption key.
    • setKey() Sets the encryption key.

    Signing

    To instruct the component to use signing or not, useSigning is available. It accepts a boolean which sets a flag internally, specifying whether signing will be used or not.

    If the cipher selected is of type gcm or ccm (what the cipher name ends with), auth data is required for the component to correctly encrypt or decrypt data. The methods available for this operation are:

    • setAuthTag()
    • setAuthData()
    • setAuthTagLength() - defaults to 16

    Padding

    You can also set the padding used by the component by using setPadding(). By default the component will use PADDING_DEFAULT. The available padding constants are:

    • PADDING_ANSI_X_923
    • PADDING_DEFAULT
    • PADDING_ISO_10126
    • PADDING_PKCS7
    • PADDING_SPACE
    • PADDING_ZERO

    Dependency Injection

    As with most Phalcon components, you can store the Phalcon\Crypt object in your container. By doing so, you will be able to access your configuration object from controllers, models, views and any component that implements Injectable.

    1. <?php
    2. use Phalcon\Di\FactoryDefault;
    3. use Phalcon\Crypt;
    4. // Create a container
    5. $container = new FactoryDefault();
    6. $container->set(
    7. 'crypt',
    8. function () {
    9. // Set a global encryption key
    10. $crypt->setKey(
    11. "T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3"
    12. );
    13. return $crypt;
    14. },
    15. true
    16. );

    The component is now available in your controllers using the crypt key

    1. <?php
    2. use MyApp\Models\Secrets;
    3. use Phalcon\Crypt;
    4. use Phalcon\Http\Request;
    5. use Phalcon\Mvc\Controller;
    6. /**
    7. * @property Crypt $crypt
    8. * @property Request $request
    9. */
    10. class SecretsController extends Controller
    11. {
    12. public function saveAction()
    13. {
    14. $secret = new Secrets();
    15. $text = $this->request->getPost('text');
    16. $secret->content = $this->crypt->encrypt($text);
    17. if ($secret->save()) {
    18. $this->flash->success(
    19. 'Secret was successfully created!'
    20. );
    21. }
    22. }