Encryption and decryption in Laravel 5

2020-03-24 06:06发布

问题:

I have been looking for ideas on encrypting and decrypting values in Laravel (like VIN Numbers, Employee ID Card Numbers, Social Security Numbers, etc.) and recently found this on the Laravel website: https://laravel.com/docs/5.6/encryption

My question is, how would I print the decrypted values on a blade template? I could see going through the controller and setting a variable and then printing it to a Blade, but I was curious as to how I would also print a decrypted value to an index? Like so...

@foreach($employees as $employee)
{{$employee->decrypted value somehow}}
{{$employee->name}}
@endforeach

回答1:

You can handle encrypted attributes with a trait (app/EncryptsAttributes.php):

namespace App;

trait EncryptsAttributes {

    public function attributesToArray() {
        $attributes = parent::attributesToArray();
        foreach($this->getEncrypts() as $key) {
            if(array_key_exists($key, $attributes)) {
                $attributes[$key] = decrypt($attributes[$key]);
            }
        }
        return $attributes;
    }

    public function getAttributeValue($key) {
        if(in_array($key, $this->getEncrypts())) {
            return decrypt($this->attributes[$key]);
        }
        return parent::getAttributeValue($key);
    }

    public function setAttribute($key, $value) {
        if(in_array($key, $this->getEncrypts())) {
            $this->attributes[$key] = encrypt($value);
        } else {
            parent::setAttribute($key, $value);
        }
        return $this;
    }

    protected function getEncrypts() {
        return property_exists($this, 'encrypts') ? $this->encrypts : [];
    }

}

Use it in your models when necessary:

class Employee extends Model {

    use EncryptsAttributes;

    protected $encrypts = ['cardNumber', 'ssn'];

}

Then you can get and set the attributes without thinking about the encryption:

$employee->ssn = '123';
{{ $employee->ssn }}


回答2:

You could create a custom function or an accessor in your model.

Say your model is Employee and your encrypted column is ssn. You could do that:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    // With a function
    public function decryptSsn()
    {
        return decrypt($this->attributes['ssn']);
    }

    // With an accessor
    public function getDecryptedSsnAttribute()
    {
        return decrypt($this->attributes['ssn']);
    }
}

In case you go with function, you'd call it like this:

$employee->decryptSsn();

And if you go with an accessor, you would call it like this:

$employee->decrypted_ssn;


回答3:

use appends in models. easier to use anywhere without repeating using encrypt/decrypt helper

class Employee extends Model {
     protected $appends = [
           'encrypted_ssn_number',
     ];

     protected $hidden = ['ssn']; //if you want to hide from json of actual value of ssn

     public function getEncryptedSsnNumberAttribute()
     {
         return encrypt($this->ssn); // md5($this->ssn);  //bcrypt($this->ssn)
         // if $this->ssn not working, use $this->attribute['ssn']
     }
}

use in model

{{ employee->encrypted_ssn_number }}


回答4:

you can handle with simple create a custom trait in app/trait Encryptable.php

namespace App\Traits;
use Illuminate\Support\Facades\Crypt;

trait Encryptable
{
public function getAttribute($key)
{
    $value = parent::getAttribute($key);
    if (in_array($key, $this->encryptable)) {
        try {
            $value = Crypt::decrypt($value);
        } catch (\Exception $e) {
            $value = null;
        }
    }
    return $value;
}
public function setAttribute($key, $value)
{
    parent::setAttribute($key, $value);
    $value = $this->attributes[$key];
    if (in_array($key, $this->encryptable)) {
        $this->attributes[$key] = Crypt::encrypt($value);
    }
    return $this;
}
}

Use it in your models only those columns which you want to encryption.

use App\Traits\Encryptable;
class ABC extends Model {

use Encryptable;

protected $encryptable= ['name', 'description'];

}


回答5:

create file Encryptable.php inside app/Traits

<?php

namespace App\Traits;
use Crypt;

trait Encryptable
{

     public function toArray()
     {
        $array = parent::toArray();
        foreach ($array as $key => $attribute) {
            if (in_array($key, $this->encryptable) && $array[$key]!='') {
                try {
                $array[$key] = Crypt::decrypt($array[$key]);
               } catch (\Exception $e) {
               }
            }
        }
        return $array;
    }

    public function getAttribute($key)
    {
        try {
            $value = parent::getAttribute($key);
            if (in_array($key, $this->encryptable) && $value!='') {
                $value = Crypt::decrypt($value);
                return $value;
            }
            return $value;
        } catch (\Exception $e) {
            return $value;
        }
    }

    public function setAttribute($key, $value)
    {
        if (in_array($key, $this->encryptable)) {
            $value = Crypt::encrypt($value);
        }
        return parent::setAttribute($key, $value);
    }
}

inside your model :

use App\Traits\Encryptable;

class Employee extends Model {

    use Encryptable;
    protected $encryptable = ['cardNumber', 'ssn'];

}


回答6:

Create one helper file and inside that file create function that will be accessible from any views. Follow this link for create helper: https://laravelcode.com/post/how-to-create-custom-helper-in-laravel-55

function decryptText($text) {
   return decrypt($text);
}

and use inside view like this :

@foreach($employees as $employee)
 {{decryptText($employee->encryptedText)}}
 {{$employee->name}}
@endforeach