Laravel 5中的加密和解密

时间:2018-03-27 01:53:48

标签: php laravel encryption laravel-5

我一直在寻找关于加密和解密Laravel值的想法(如VIN号码,员工身份证号码,社会安全号码等),最近在Laravel网站上找到了这个:https://laravel.com/docs/5.6/encryption

我的问题是,如何在刀片模板上打印解密的值?我可以看到通过控制器并设置变量然后将其打印到Blade,但我很好奇我将如何将解密值打印到索引?像这样......

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

6 个答案:

答案 0 :(得分:4)

您可以使用特征(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 : [];
    }

}

必要时在模特中使用它:

class Employee extends Model {

    use EncryptsAttributes;

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

}

然后,您可以在不考虑加密的情况下获取和设置属性:

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

答案 1 :(得分:0)

您可以在模型中创建自定义函数或an accessor

假设您的模型为Employee,加密列为ssn。你可以这样做:

<?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']);
    }
}

如果您使用功能,您可以这样称呼它:

$employee->decryptSsn();

如果你使用访问器,你可以这样称呼它:

$employee->decrypted_ssn;

答案 2 :(得分:0)

在模型中使用appends。更容易在任何地方使用而无需重复使用加密/解密助手

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']
     }
}

在模型中使用

{{ employee->encrypted_ssn_number }}

答案 3 :(得分:0)

您可以在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 App\Traits\Encryptable;
class ABC extends Model {

use Encryptable;

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

}

答案 4 :(得分:0)

Encryptable.php内创建文件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);
    }
}

在模型内部:

use App\Traits\Encryptable;

class Employee extends Model {

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

}

答案 5 :(得分:-2)

创建一个帮助文件,并在该文件中创建可从任何视图访问的函数。 请点击此链接创建帮助者:https://laravelcode.com/post/how-to-create-custom-helper-in-laravel-55

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

并使用内部视图:

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