代码之家  ›  专栏  ›  技术社区  ›  user2197774

Laravel加密字符串未按预期运行

  •  0
  • user2197774  · 技术社区  · 3 年前

    我有以下代码:

    $user = User::where('UserID', 14)->first();
    $encrypted=Crypt::encryptString('MySecretPassword');
    $user->smtp_password=$encrypted;
    $user->save();
    
    //This outputs the original password MySecretPassword.
    dd($user->smtp_password);
    //This gives payload is invalid error
    $passw=Crypt::decryptString($user->smtp_password);
    

    原始加密(Crypt::encryptString)将MySecretPassword保存为Mysql数据库中的长加密字符串。数据库数据类型为长文本。当我在不解密的情况下输出值时,它会给我原始密码形式,而不是加密的字符串(请参阅上面的代码)。为什么我看不到数据库中的加密字符串?我还没有进行任何解密。

    用户模型:

    namespace App;
    
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Auth;
    use Crypt;
    
    class User extends Authenticatable
    {
        use Notifiable;
        
        
    protected $primaryKey = 'UserID';
    public $timestamps = false;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password','smtp_password',
        ];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 
            'remember_token', 
            'driver', 
            'host', 
            'port', 
            'encryption',
            'username',
            'smtp_password'
        ];
    
        /**
         * The attributes that should be cast to native types.
         *
         * @var array
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
        
        
        
        public function getSmtpPasswordAttribute($value)
        {
            return Crypt::decryptString($value);
        }
    
        
        
    public function scopeConfiguredEmail($query) {
    $user = Auth::user();
    return $query->where('UserID', $user->UserID);
    }
    
    0 回复  |  直到 3 年前