Encryption

I have already data in a database that is not encrypted. Now I have decided that some of the data should be encrypted and I thought the best way to do it is to use CSecurityManager. How can I encrypt the already existing data in database?

Try moving the unencrypted data attribute to a temporary variable; next encrypt that temporary variable; then save the encrypted result into the original data attribute. Be careful that the column definition supports the encrypted value (size, charset, etc.).

Hi, Thank you for the reply. What should the size and charset be, if I use CSecurityManager?

There is said in CSecurityManager documentation that "PHP Mcrypt extension must be installed and loaded". How do I do that?

I’m trying to encrypt the name in beforeSave function in my model, but it doesn’t encrypt. What might be the problem?




public function beforeSave()

    {

        Yii::app()->getSecurityManager()->encrypt($this->name); 

                                

        return parent::beforeSave();

    } 



Ok, I found the problem. It should be




$this->name = Yii::app()->getSecurityManager()->encrypt($this->name);



Why do I get an empty string when I use this




 public function afterFind()

    {


        $eKey = Yii::app()->getSecurityManager()->getEncryptionKey();

       

        $this->name = Yii::app()->getSecurityManager()->decrypt($this->name, $eKey); 

        

        return parent::afterFind();

    }



Ok, this seemed to do the trick. I changed in database the character set to UTF8 of name field and used these in my model class:





 public function afterFind()

    {


        $eKey = Yii::app()->getSecurityManager()->getEncryptionKey();

       

        $this->name = Yii::app()->getSecurityManager()->decrypt(utf8_decode($this->name), $eKey); 

        

        return parent::afterFind();

    }


    public function beforeSave()

    {

        

        $eKey = Yii::app()->getSecurityManager()->getEncryptionKey();


        $this->name = utf8_encode(Yii::app()->getSecurityManager()->encrypt($this->name, $eKey)); 

                                

        return parent::beforeSave();

    } 



and in main.php this







 'components' => array(

        

        'securityManager'=>array(

            'cryptAlgorithm' => 'blowfish',

            'encryptionKey' => 'Put here your secret key',

        ),