dongyi6183
2018-03-27 01:53 阅读 476
已采纳

Laravel 5中的加密和解密

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
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • 已采纳
    dongyun4010 dongyun4010 2018-03-27 15:20

    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 }}
    
    点赞 评论 复制链接分享
  • dsgwii4867 dsgwii4867 2018-03-27 02:03

    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;
    
    点赞 评论 复制链接分享
  • doufang3001 doufang3001 2018-03-27 03:34

    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 }}
    
    点赞 评论 复制链接分享
  • douqiang1851 douqiang1851 2018-03-27 07:28

    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
    
    点赞 评论 复制链接分享
  • dtfo55908 dtfo55908 2018-07-13 11:07

    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'];
    
    }
    
    点赞 评论 复制链接分享

相关推荐