加密与解密Encryption/Decryption

Phalcon通过 Phalcon\Crypt 组件提供了加密和解密工具。这个类提供了对php mcrypt 的封装。默认情况下这个组件使用AES-256 (rijndael-256-cbc)。

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

By default, this component provides secure encryption using AES-256 (rijndael-256-cbc).

基本使用Basic Usage

这个组件极易使用:

This component is designed to provide a very simple usage:

  1. <?php
  2. use Phalcon\Crypt;
  3. //Create an instance
  4. $crypt = new Crypt();
  5. $key = 'le password';
  6. $text = 'This is a secret text';
  7. $encrypted = $crypt->encrypt($text, $key);
  8. echo $crypt->decrypt($encrypted, $key);

也可以使用同一实例加密多次:

You can use the same instance to encrypt/decrypt several times:

  1. <?php
  2. use Phalcon\Crypt;
  3. //Create an instance
  4. $crypt = new Crypt();
  5. $texts = array(
  6. 'my-key' => 'This is a secret text',
  7. 'other-key' => 'This is a very secret'
  8. );
  9. foreach ($texts as $key => $text) {
  10. //Perform the encryption
  11. $encrypted = $crypt->encrypt($text, $key);
  12. //Now decrypt
  13. echo $crypt->decrypt($encrypted, $key);
  14. }

加密选项Encryption Options

下面的选项可以改变加密的行为:

The following options are available to change the encryption behavior:

NameDescription
CipherThe cipher is one of the encryption algorithms supported by libmcrypt. You can see a list here
ModeOne of the encryption modes supported by libmcrypt (ecb, cbc, cfb, ofb)

例子:

Example:

  1. <?php
  2. use Phalcon\Crypt;
  3. //Create an instance
  4. $crypt = new Crypt();
  5. //Use blowfish
  6. $crypt->setCipher('blowfish');
  7. $key = 'le password';
  8. $text = 'This is a secret text';
  9. echo $crypt->encrypt($text, $key);

支持Base64 Base64 Support

为了方便传输或显示我们可以对加密后的数据进行 base64 转码:

In order that encryption is properly transmitted (emails) or displayed (browsers) base64 encoding is usually applied to encrypted texts:

  1. <?php
  2. use Phalcon\Crypt;
  3. //Create an instance
  4. $crypt = new Crypt();
  5. $key = 'le password';
  6. $text = 'This is a secret text';
  7. $encrypt = $crypt->encryptBase64($text, $key);
  8. echo $crypt->decryptBase64($text, $key);

配置加密服务Setting up an Encryption service

你也可以把加密组件放入服务容器中这样我们可以在应用中的任何一个地方访问这个组件:

You can set up the encryption component in the services container in order to use it from any part of the application:

  1. <?php
  2. use Phalcon\Crypt;
  3. $di->set('crypt', function() {
  4. $crypt = new Crypt();
  5. //Set a global encryption key
  6. $crypt->setKey('%31.1e$i86e$f!8jz');
  7. return $crypt;
  8. }, true);

然后,例如,我们可以在控制器中使用它了:

Then, for example, in a controller you can use it as follows:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class SecretsController extends Controller
  4. {
  5. public function saveAction()
  6. {
  7. $secret = new Secrets();
  8. $text = $this->request->getPost('text');
  9. $secret->content = $this->crypt->encrypt($text);
  10. if ($secret->save()) {
  11. $this->flash->success('Secret was successfully created!');
  12. }
  13. }
  14. }