Laravel 5 中的加密和解密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49503230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encryption and decryption in Laravel 5
提问by Matthew
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
我一直在寻找关于在 Laravel 中加密和解密值的想法(如 VIN 号码、员工身号码、社会安全号码等),最近在 Laravel 网站上找到了这个:https://laravel.com/docs/5.6 /加密
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...
我的问题是,如何在刀片模板上打印解密的值?我可以看到通过控制器并设置一个变量,然后将其打印到 Blade,但我很好奇如何将解密的值打印到索引?像这样...
@foreach($employees as $employee)
{{$employee->decrypted value somehow}}
{{$employee->name}}
@endforeach
回答by Jonas Staudenmeir
You can handle encrypted attributes with a trait (app/EncryptsAttributes.php
):
您可以使用特征 ( 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 }}
回答by DevK
You could create a custom function or an accessorin your model.
您可以在模型中创建自定义函数或访问器。
Say your model is Employee
and your encrypted column is ssn
. You could do that:
假设您的模型是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']);
}
}
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;
回答by ZeroOne
use appends
in models. easier to use anywhere without repeating using encrypt/decrypt helper
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']
}
}
use in model
在模型中使用
{{ employee->encrypted_ssn_number }}
回答by Rahul Tathod
you can handle with simple create a custom trait in app/trait Encryptable.php
您可以在 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'];
}
回答by Saurabh Mistry
create file Encryptable.php
inside app/Traits
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);
}
}
inside your model :
在您的模型中:
use App\Traits\Encryptable;
class Employee extends Model {
use Encryptable;
protected $encryptable = ['cardNumber', 'ssn'];
}
回答by narayansharma91
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
创建一个帮助文件,并在该文件中创建可从任何视图访问的函数。按照此链接创建助手: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