2012-08-07 4 views
0

내 모델의 beforeSave 메소드에서 일부 데이터를 암호화하려고합니다. 그러나 구원받지 못하고 있습니다.CakePHP 2.0 Security :: 암호 결과가 저장되지 않습니다.

 $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 문자를 생성하고 있다는 것입니다. 중국어 문자 등. 내가 한 것은 암호화 된 문자열을 16 진수로 변환 한 다음 저장하는 것입니다. 그리고 데이터를 다시 가져올 때 헥사에서 문자로 디코딩 한 다음 해독합니다. (AppModel.php IN)

$ this-> CipherKey이 모델

<?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); 
} 
?> 
으로 동일한 방식으로 __construct에로드되는 복호화 방법에있어서, (AppController.php IN)
<?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)); 
} 
?>