目录 搜索
欢迎 目录 快速参考图 基本信息 服务器要求 许可协议 变更记录 关于CodeIgniter 安装 下载 CodeIgniter 安装指导 从老版本升级 疑难解答 介绍 开始 CodeIgniter 是什么? CodeIgniter 速记表 支持特性 应用程序流程图 模型-视图-控制器 架构目标 教程 内容提要 加载静态内容 创建新闻条目 读取新闻条目 结束语 常规主题 CodeIgniter URL 控制器 保留字 视图 模型 辅助函数 使用 CodeIgniter 类库 创建你自己的类库 使用 CodeIgniter 适配器 创建适配器 创建核心系统类 钩子 - 扩展框架的核心 自动装载资源 公共函数 URI 路由 错误处理 缓存 调试应用程序 以CLI方式运行 管理应用程序 处理多环境 PHP替代语法 安全 开发规范 类库参考 基准测试类 日历类 购物车类 配置类 Email 类 加密类 文件上传类 表单验证详解 FTP 类 图像处理类 输入类 Javascript 类 语言类 装载类 迁移类 输出类 分页类 模板解析器类 安全类 Session 类 HTML 表格类 引用通告类 排版类 单元测试类 URI 类 User-Agent 类 表单验证 XML-RPC 和 XML-RPC 服务器 Zip 编码类 缓存适配器 适配器参考 适配器 数据库类 Active Record 类 数据库缓存类 自定义函数调用 数据库配置 连接你的数据库 数据库快速入门例子代码 字段数据 数据库维护类 查询辅助函数 数据库类 查询 生成查询记录集 表数据 事务 数据库工具类 JavaScript类 辅助函数参考 数组辅助函数 CAPTCHA 辅助函数 Cookie Helper 日期辅助函数 目录辅助函数 下载辅助函数 Email 辅助函数 文件辅助函数 表单辅助函数 HTML辅助函数 Inflector 辅助函数 语言辅助函数 数字辅助函数 路径辅助函数 安全辅助函数 表情辅助函数 字符串辅助函数 文本辅助函数 排版辅助函数 URL 辅助函数 XML 辅助函数
文字

CodeIgniter 用户指南 版本 2.1.0

编辑文档、查看近期更改请 登录 或 注册  找回密码
查看原文

加密类

数据加密类提供了两种数据加密方式。 It uses a scheme that either compiles the message using a randomly hashed bitwise XOR encoding scheme, or is encrypted using the Mcrypt library. If Mcrypt is not available on your server the encoded message will still provide a reasonable degree of security for encrypted sessions or other such "light" purposes. If Mcrypt is available, you'll be provided with a high degree of security appropriate for storage.

设置你的密钥

密钥实际上是一些会控制密码加密过程并且允许被加密的字串被解码的信息片段。实际上,你选择的密钥会提供一个唯一的方法来解密一些被加密的数据,所以你需要非常谨慎的设置你的密钥,如果你想给一些固定的数据加密的话,你最好不要更改这个密钥。

很自然,你需要非常小心的保守你的密钥。如果某人对您的密钥能够存取,那么数据将会很容易地被解码。如果您的服务器不完全在的您的控制之下而想保证数据安全是不可能的,因此您可以在使用它之前仔细地想一下要求高安全存放信用卡数字对象的方法。

为了发挥加密算法的最大优势,你的解密密钥需要被设置为 32 个字符长度(128 位)。你可以设置一个编造的随机字符串作为你的密钥,最好包括数字、大写字母、小写字母。你的密钥不能设置为一个简单的文本字符串。为了被安全可靠的加密,它也有一个随机的可能性。

你的密钥可以放在 application/config/config.php 文件中,你也可以自己设置一个存储机制用于数据的加密和解密。

为了在 application/config/config.php 文件中保存你的密钥,打开文件设置一下:

$config['encryption_key'] = "YOUR KEY";

消息长度

知道加密信息的长度会是原来函数长度的 2.6 倍是很重要的。如果你加密这个字符串“my super secret data”,它的长度是 21 个字符,所以你加密后的字符串的长度大概是 55 个字符(我们说它是粗糙的,因为编码的字符串长度增量 64 位并非是线性增长的),当你选择你的数据存储机制的时候一定要记住这一点。例如,Cookie 可以占用 4k 的数据空间。

初始化类

在 Codeigniter 中,像大多数其他的类一样,加密类也需要在你的控制器函数中用 $this->load->library 函数加载:

$this->load->library('encrypt');

一旦被加载,加密类库就可以这样使用:$this->encrypt

$this->encrypt->encode()

执行数据加密并返回一个字符串。例如:

$msg = 'My secret message';

$encrypted_string = $this->encrypt->encode($msg);

如果你不想在你的配置文件中使用一个密钥,你可以通过第二个参数随意设置你的密钥。

$msg = 'My secret message';
$key = 'super-secret-key';

$encrypted_string = $this->encrypt->encode($msg, $key);

$this->encrypt->decode()

解密一个已加密的字符串。例如:

$encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';

$plaintext_string = $this->encrypt->decode($encrypted_string);

You can optionally pass your encryption key via the second parameter if you don't want to use the one in your config file:

$msg = 'My secret message';
$key = 'super-secret-key';

$encrypted_string = $this->encrypt->decode($msg, $key);

$this->encrypt->set_cipher();

允许你设置一个 Mcrypt 算法。默认使用 MCRYPT_RIJNDAEL_256。例如:

$this->encrypt->set_cipher(MCRYPT_BLOWFISH);

请访问 php.net 看一下可用的算法。

如果你想手动测试一下你的服务器是否支持 Mcrypt,你可以使用:

echo ( ! function_exists('mcrypt_encrypt')) ? 'Nope' : 'Yup';

$this->encrypt->set_mode();

允许你设置一个 Mcrypt 模式。默认使用 MCRYPT_MODE_CBC。例如:

$this->encrypt->set_mode(MCRYPT_MODE_CFB);

请访问 php.net 看一下可用的模式。

$this->encrypt->sha1();

SHA1 编码函数。提供一个字符串,然后它返回一个 160 位的 Hash 信息。说明:SHA1,就像 MD5 一样不可解密。例如:

$hash = $this->encrypt->sha1('Some string');

许多 PHP 安装程序默认都支持 SHA1,所以你可以很简单的使用它的原始函数进行加密:

$hash = sha1('Some string');

如果你的服务器不支持 SHA1,你可以使用别人提供的函数。

$this->encrypt->encode_from_legacy($orig_data, $legacy_mode = MCRYPT_MODE_ECB, $key = '');

Enables you to re-encode data that was originally encrypted with CodeIgniter 1.x to be compatible with the Encryption library in CodeIgniter 2.x. It is only necessary to use this method if you have encrypted data stored permanently such as in a file or database and are on a server that supports Mcrypt. "Light" use encryption such as encrypted session data or transitory encrypted flashdata require no intervention on your part. However, existing encrypted Sessions will be destroyed since data encrypted prior to 2.x will not be decoded.

Why only a method to re-encode the data instead of maintaining legacy methods for both encoding and decoding? The algorithms in the Encryption library have improved in CodeIgniter 2.x both for performance and security, and we do not wish to encourage continued use of the older methods. You can of course extend the Encryption library if you wish and replace the new methods with the old and retain seamless compatibility with CodeIgniter 1.x encrypted data, but this a decision that a developer should make cautiously and deliberately, if at all.

$new_data = $this->encrypt->encode_from_legacy($old_encrypted_string);
Parameter Default Description
$orig_data n/a The original encrypted data from CodeIgniter 1.x's Encryption library
$legacy_mode MCRYPT_MODE_ECB The Mcrypt mode that was used to generate the original encrypted data. CodeIgniter 1.x's default was MCRYPT_MODE_ECB, and it will assume that to be the case unless overridden by this parameter.
$key n/a The encryption key. This it typically specified in your config file as outlined above.

 

翻译贡献者: baiyuxiong, Hex, qixingyue, zhupeng
最后修改: 2011-04-06 23:38:13
上一篇: 下一篇: