php - Decrypt and Encrypt using CallBack Methods in cakephp -
i want use callbacks methods encrypt value before gets stored in database , decrypt before showing in application.
i used 1 of examples provided in documentation.
in core.php
put following :
configure::write('security.ciphercriptkey','su0hksspmdbwgk6ldqlqzp0ymyati7zo');
in model, used 2 methods:
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; }
afterfind()
public function afterfind($results, $primary = false) { foreach ($results $key => $val) { if(isset($val['internship']['encryptedindb'])){ $results['internship']['encryptedindb'] = security::decrypt($val['internship']['encryptedindb'], configure::read('security.ciphercriptkey')); } return $results; } }
the beforesave()
seems working fine, since can see in database value encrypted. however, in view, , when see content of field decrypted, displays empty field. if afterfind()
method unable decrypt (it returns false).
below screenshot of application's view:
and database values encrypted:
the function security::encrypt($text)
uses aes-256 algorithm encrypt $text
. returns binary data, , such, should stored in binary data type, instead of text type.
any of following should work:
- binary
- varbinary
- blob (tinyblob, blob, mediumblob, , longblob).
setting varbinary(255)
should enough.
for further reference, see:
Comments
Post a Comment