# Encryption/Decryption
Phalcon通過[Phalcon\\Crypt](http://docs.iphalcon.cn/api/Phalcon_Crypt.html)組件提供了加密和解密工具。這個類提供了對PHP[openssl](http://www.php.net/manual/en/book.openssl.php)的封裝。
默認情況下這個組件使用AES-256-CFB。
> You must use a key length corresponding to the current algorithm. For the algorithm used by default it is 32 bytes.
## 基本使用
這個組件極易使用:
~~~
<?php
use Phalcon\Crypt;
// Create an instance
$crypt = new Crypt();
$key = "This is a secret key (32 bytes).";
$text = "This is the text that you want to encrypt.";
$encrypted = $crypt->encrypt($text, $key);
echo $crypt->decrypt($encrypted, $key);
~~~
也可以使用同一實例加密多次:
~~~
<?php
use Phalcon\Crypt;
// 創建實例
$crypt = new Crypt();
$texts = [
"my-key" => "This is a secret text",
"other-key" => "This is a very secret",
];
foreach ($texts as $key => $text) {
// 加密
$encrypted = $crypt->encrypt($text, $key);
// 解密
echo $crypt->decrypt($encrypted, $key);
}
~~~
## 加密選項(Encryption Options)
下面的選項可以改變加密的行為:
| 名稱 | 描述 |
| --- | --- |
| Cipher | cipher是libmcrypt提供支持的一種加密算法。 查看這里[here](http://www.php.net/manual/en/function.openssl-get-cipher-methods.php) |
例子:
~~~
<?php
use Phalcon\Crypt;
// 創建實例
$crypt = new Crypt();
// 使用 blowfish
$crypt->setCipher("bf-cbc");
$key = "le password";
$text = "This is a secret text";
echo $crypt->encrypt($text, $key);
~~~
## 提供 Base64(Base64 Support)
為了方便傳輸或顯示我們可以對加密后的數據進行[base64](http://www.php.net/manual/en/function.base64-encode.php)轉碼:
~~~
<?php
use Phalcon\Crypt;
// 創建實例
$crypt = new Crypt();
$key = "le password";
$text = "This is a secret text";
$encrypt = $crypt->encryptBase64($text, $key);
echo $crypt->decryptBase64($encrypt, $key);
~~~
## 配置加密服務(Setting up an Encryption service)
你也可以把加密組件放入服務容器中這樣我們可以在應用中的任何一個地方訪問這個組件:
~~~
<?php
use Phalcon\Crypt;
$di->set(
'crypt',
function () {
$crypt = new Crypt();
// 設置全局加密密鑰
$crypt->setKey(
"%31.1e$i86e$f!8jz"
);
return $crypt;
},
true
);
~~~
然后,例如,我們可以在控制器中使用它了:
~~~
<?php
use Phalcon\Mvc\Controller;
class SecretsController extends Controller
{
public function saveAction()
{
$secret = new Secrets();
$text = $this->request->getPost("text");
$secret->content = $this->crypt->encrypt($text);
if ($secret->save()) {
$this->flash->success(
"Secret was successfully created!"
);
}
}
}
~~~
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl