Qar*_*ail 2 php encryption cakephp cakephp-2.x
我希望使用Callbacks方法在将值存储到我的数据库之前对其进行加密,然后在将值显示回应用程序之前对其进行解密.
我使用了文档中提供的一个示例.
在我,core.php我把以下内容:
Configure::write('Security.cipherCriptKey','su0HKssPmdbwgK6LdQLqzp0YmyaTI7zO');
Run Code Online (Sandbox Code Playgroud)
在我的模型中,我使用了两种方法:
beforeSave()
public function beforeSave($options = array()) {
$value=$this->data['Internship']['encryptedindb'];
$encrypted = Security::encrypt($value, Configure::read('Security.cipherCriptKey'));
$this->data['Internship']['encryptedindb'] = $encrypted;
return true;
}
Run Code Online (Sandbox Code Playgroud)afterFind()
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
if(isset($val['Internship']['encryptedindb'])){
$results['Internship']['encryptedindb'] = Security::decrypt($val['Internship']['encryptedindb'], Configure::read('Security.cipherCriptKey'));
}
return $results;
}
}
Run Code Online (Sandbox Code Playgroud)在beforeSave()似乎是做工精细,因为我可以在我的数据库加密的价值看.但是,在我看来,当我希望看到该字段的内容被解密时,它会将其显示为空字段.好像该afterFind()方法无法解密(它返回总是false).
以下是我的应用程序视图的屏幕截图:
和加密值的数据库:
该功能Security::encrypt($text)使用AES-256算法进行加密$text.它返回二进制数据,因此,它应存储在二进制数据类型中,而不是文本类型.
以下任何一项都应该有效:
设置它VARBINARY(255)应该足够了.
有关进一步参考,请参阅: