Searching Encrypted Data in GridView

Hello,

I need direction on how to search for encrypted data in gridview. I am able to encrypt/decrypt data in my model.




public function beforeSave($insert) {

        $inputKey = Yii::$app->params['edKey'];

        if (parent::beforeSave($insert)) {

            $this->firstname = Yii::$app->security->encryptByKey($this->firstname, $inputKey);

            $this->lastname = Yii::$app->security->encryptByKey($this->lastname, $inputKey);

            $this->othernames = Yii::$app->security->encryptByKey($this->othernames, $inputKey);


            return true;

        } else {

            return false;

        }

    }


    public function afterFind() {




        parent::afterFind();

        $inputKey = Yii::$app->params['edKey'];

        $this->firstname = Yii::$app->security->decryptByKey($this->firstname, $inputKey);

        $this->lastname = Yii::$app->security->decryptByKey($this->lastname, $inputKey);

        $this->othernames = Yii::$app->security->decryptByKey($this->othernames, $inputKey);

    }



However, when searching encrypted fields(firstname, lastname and othernames) in gridview, I get zero results yet the data exists. Here is my current search model:




class PersonSearch extends Person

{

    /**

 	* @inheritdoc

 	*/

    public function rules()

    {

        return [

            [['id'], 'integer'],

            [['firstname', 'lastname', 'othernames', 'created_at', 'updated_at'], 'safe'],

        ];

    }


    /**

 	* @inheritdoc

 	*/

    public function scenarios()

    {

        // bypass scenarios() implementation in the parent class

        return Model::scenarios();

    }


    /**

 	* Creates data provider instance with search query applied

 	*

 	* @param array $params

 	*

 	* @return ActiveDataProvider

 	*/

    public function search($params)

    {

        $query = Person::find();


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


        $this->load($params);


        if (!$this->validate()) {

            // uncomment the following line if you do not want to return any records when validation fails

            // $query->where('0=1');

            return $dataProvider;

        }


        $query->andFilterWhere([

            'id' => $this->id,

            'created_at' => $this->created_at,

            'updated_at' => $this->updated_at,

        ]);


        $query->andFilterWhere(['like', 'firstname', $this->firstname])

            ->andFilterWhere(['like', 'lastname', $this->lastname])

            ->andFilterWhere(['like', 'othernames', $this->othernames]);


        return $dataProvider;

    }

}




I believe I am suppose to modify my search model but I need guidance on how to go about it.

I’m not sure if you can search anything like that. To use ‘like’ filter you would have to decrypt every db table row and compare if to the sent string.

For exact matches you can create an index of hashes from real data and calculate a hash from query string when searched. For non-exact matches you have to decrypt it, I guess.

Hello, did you find a way to do it? I’m facing the same problem. Kind regards