php Laravel 5中的加密和解密

tcbh2hod  于 2023-05-12  发布在  PHP
关注(0)|答案(7)|浏览(163)

我一直在寻找在Laravel中加密和解密值的想法(如VIN号码,员工身份证号码,社会安全号码等),最近在Laravel网站上找到了这个:https://laravel.com/docs/5.6/encryption
我的问题是,如何在刀片模板上打印解密值?我可以看到通过控制器设置一个变量,然后将其打印到一个刀片,但我很好奇,我如何也将解密值打印到一个索引?像这样...

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

eoigrqb61#

你可以使用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 : [];
    }

}

必要时在模型中使用它:

class Employee extends Model {

    use EncryptsAttributes;

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

}

然后,您可以获取和设置属性,而无需考虑加密:

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

velaa5lx2#

您可以在模型中创建自定义函数或访问器。
假设你的模型是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']);
    }
}

如果你使用function,你可以这样调用它:

$employee->decryptSsn();

如果你使用一个访问器,你会这样调用它:

$employee->decrypted_ssn;
6vl6ewon

6vl6ewon3#

在模型中使用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 }}
wgx48brx

wgx48brx4#

你可以在app/trait Encryptable.php中创建一个自定义trait

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

}
56lgkhnf

56lgkhnf5#

app/Traits内部创建文件Encryptable.php

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

}
jbose2ul

jbose2ul6#

使用URL中的保存id Laravel加密和解密此方法

{{encrypt($edit_template->id)}}  

{{decrypt($id)}}
a5g8bdjr

a5g8bdjr7#

创建一个helper文件,并在该文件中创建可以从任何视图访问的函数。按照此链接创建帮助程序: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

相关问题