2012-08-07 45 views
0

我正在嘗試加密模型的beforeSave方法中的一些數據。但它沒有被保存。CakePHP 2.0安全::密碼結果不被保存

 $currentBalance = $this->find("all", array(
      "fields" => array(
       "SUM(base_amount) as 'CurrentBalance'" 
      ), 
      "conditions" => array(
       "Payment.user_id" => $this->data["Payment"]["user_id"] 
      ) 
     )); 

     $this->log($this->data["Payment"]); 
     $this->log(Configure::read("Security.salt"));   
     $this->log(Security::cipher(implode("|", $this->data["Payment"]), Configure::read("Security.cipherSeed"))); 

     $this->set("balance", $currentBalance[0][0]["CurrentBalance"] + $this->data["Payment"]["base_amount"]); 
     $this->set("balance_checksum", Security::cipher(implode("|", $this->data["Payment"]), Configure::read("Security.salt"))); 

如果我查看日誌文件,我會得到某種加密數據,但它都是giberish。

雖然在數據庫中我什麼都沒有得到。

如果我用一個簡單的字符串替換密碼函數說「123」...這是得到正確保存。

我已經確保數據庫連接是utf8編碼,並且數據庫中的字段具有utf8歸類。

在這個任何指針將是巨大的

感謝

回答

4

我已經對類似的問題。問題在於加密數據造成了一些無效的utf8字符。比如漢字和東西。我所做的是將加密的字符串轉換爲十六進制然後保存。當獲取數據時,您將從六進制解碼爲字符,然後對其進行解密。

(在AppModel.php)

<?php 
/** 
* Encrypts a sensible string for the bd 
* @param string $toEncrypt 
* @return string encrypted hexadecimal 
*/ 
function encryptCipher($toEncrypt) { 
    $CipherKey = Configure::read('Security.cipherSeed'); 
    return bin2hex(Security::cipher($toEncrypt, $CipherKey)); 
} 
?> 

(在AppController.php)解密方法,其中$這 - >密碼索引被裝入__construct的確切相同的方式模型

<?php function __construct(){ 
$this->CipherKey = Configure::read('Security.cipherSeed'); 
} 

/** 
* The main decrypting function 
* 
* @param string $strToDecrypt the string to be decrypted 
* @return string the decrypted string 
*/ 
public function decryptCipher($strToDecrypt) { 
    return Security::cipher(pack("H*", $strToDecrypt), $this->CipherKey); 
} 
?>