Search Encrypted Data

chankl78 posted 4 years ago

Is there any way to search encrypted data?

I have tried as below, but always end up as record not found.

// In my controller
$i = model::where('nric', '=', $value)

In my model, I have already set as below

// In my model
public function getNricAttribute($value)
        return Crypt::decrypt($value);;

public function setNricAttribute($value)
	$this->attributes['nric'] = Crypt::encrypt($value);

Anyone can advice me? Thanks.

zenry replied 4 years ago
$i = model::where('nric', '=', Crypt::encrypt($value));
chankl78 replied 4 years ago

Thanks for the reply.

I thought the encrypt value is always changing when we save into the database due to the random $IV in the encryption?

Anyway, i did try. The record is still not able to be found.

zenry replied 4 years ago Solution

yes you a right, forgot about that

you can try this though

$nric = 'test';

$items = Model::all()->filter(function($record) use($nric) {
    if(Crypt::decrypt($record->nric) == $nric) {
        return $record;
chankl78 replied 4 years ago

Thanks!! It works beautifully. Except that, we do not need to include Crypt::decrypt() for my case as I have already defined it in the model.

bichinl replied 3 years ago

I've trying to do the same thing, except using pagination...

$nric = 'test';
$items = Model::paginate(10)->filter(function($record) use($nric) {
    if($record->name == $nric) {
        return $record;

But it only make the comparison in the pagination '10' records, I understand that could be achieved like this...

$nric = 'test';
$items = Model::get()->filter(function($record) use($nric) {
    if($record->name == $nric) {
        return $record;

Got this: "Call to undefined method Illuminate\Database\Eloquent\Collection::paginate() "

Please, could some one help me to resolve this?

ajay04bairagi replied 1 month ago

I've trying to find encrypted data using pagination but not working.

Model::paginate(10)->filter(function($record) use($request) {
	if (empty($request->input('firstname'))) {
                               return $record;
      } else if (!empty($record->firstname) && !empty($request->input('firstname')) && Crypt::decrypt($record->to) == $request->input('firstname')) {
                      return $record;

please help me to resolve this

Francesco Tesone replied 2 weeks ago

You have to use LengthAwarePaginator class. See Laravel docs

Example below.

public function index(Request $request)
        $query = $request->get('search');

        $patients = Patient::all()->filter(function($record) use ($query){
            return (strpos(Crypt::decrypt($record->name),$query) !== false) ? $record : null;

        $count = count($patients);
        $page = (request('page'))?:1;
        $rpp =  3; //(request('perPage'))?:50;
        $offset = $rpp * ($page - 1);
        $paginator = new LengthAwarePaginator($patients->slice($offset,$rpp),$count,$rpp,$page,[
            'path'  => $request->url(),
            'query' => $request->query(),
        $patients = $paginator;

        return view('admin.index',compact('patients'));
ajay04bairagi replied 1 week ago

Thanks for reply It is working fine with pagination.

Francesco Tesone replied 1 week ago

Solution it's working but it must be told, it is not the best if not the worst, because it has to get all records first, then decrypt each one and test against your matching conditions. A blind index could be one of the solutions out of many.

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us