Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sid405's avatar
Level 27

Dealing with DecryptException('Invalid data.');

Hello guys.

I'm in a bit of a situation here so i thought i'd ask for a hand.

Currently working on data intensive an application that will need caching. When i use 'file' or even 'memcached' as storage for the cache, everything is fine and dandy. the problem is i need to use 'database'.

The keys are getting correctly stored into the cache table, and even identified correctly with has(). When i then try to get the value for the key, i get the following error:

Illuminate \ Encryption \ DecryptException 
Invalid data.
Open: /Applications/MAMP/htdocs/youandbio.com/vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php
        $payload = json_decode(base64_decode($payload), true);
 
        // If the payload is not valid JSON or does not have the proper keys set we will
        // assume it is invalid and bail out of the routine since we will not be able
        // to decrypt the given value. We'll also check the MAC for this encryption.
        if ( ! $payload || $this->invalidPayload($payload))
        {
            throw new DecryptException('Invalid data.');
        }
 

Here's the code of the class that does the storing and the fetching:

<?php namespace Acme\Cache;

use Illuminate\Cache\CacheManager;

class LaravelCache implements CacheInterface {

     protected $cache;

     public function __construct(CacheManager $cache)
     {
         $this->cache = $cache;
     }

     public function get($key)
     {
        return $this->cache->get($key);
     }

     public function put($key, $value)
     {
        return $this->cache->forever($key, $value);
     }

     public function has($key)
     {
        return $this->cache->has($key);
     }

 }

Then on the various cache decorators are pulled up like this

if($this->cache->has($key))
        {
            return $this->cache->get($key);
        }

Any suggestions would be appreciated, because i can't see to get round this.

Thank you in advance

0 likes
15 replies
sid405's avatar
Level 27

@usman here's exported from mysql

INSERT INTO `cache` (`key`, `value`, `expiration`)
VALUES
    ('brands-$$-slug-$$-alkemilla', 'eyJpdiI6IkdIMDZBbnBxT1ZpZXNsejQzU0RuYXc9PSIsInZhbHVlIjoibmZYcmpnOFdrbURIWEdNZk50bXk5cytyR3JPR2laSVFqTVphV3ZXVVpXQzZhVUZwVUxTQnJQUjhoT0FoK0ZGWjI3UUFuUEVlZ1Jjd1wvTHJHZjY2Uks5emF5a085ZGRXRjVQdmhBY2RlUlwvclNBOGloSzNGNXFTRlpsN3BRNGtsbngwczNYYjRnNlpsZzg5UENtSlozakFrenZrYUtpT2szOWc1em13OU5kWmc1bFFkazJJTHBhNmZQVWdcL1dlMHdGRms5SkFuWCtzcGFoVDZmd
    [... more base64 here ...]
VdYa0NXb0JjcFFsc3YxOUtkOHBIU0ZzMEV0ejQ2MXd2MXE2U1wvd29zbE9yUmNuK0NPcXU3T1FEQjZDRnM2NjlFcGdmV1VmQnJrMGNYSDJET3djWFwvUjZpNXVXMGxDbGdCUjFiZ2lDQjc1TnhuaTJPYklxaXVQVEhBUVdkTFU', 1742996119);

The $$ in the keyname i plan to use later as a separator.

Thank you

usman's avatar

@sid405 can you paste a complete base64 string of the value column? Actually I want to check if the value is being stored correctly in your database.

usman's avatar

@sid405 let me clear it a bit:

[7]  $baseDecoded = base64_decode('eyJpdiI6IkdUNHBhMDdrRStzQ0RtRkN5blNheWc9PSIsInZhbHVlIjoiZjBDVlpGS28rMFwvbmdiY0FaaUVWb1E9PSIsIm1hYyI6Ijk1NWQzN2QzZmQwNmU3YzY2NDc2N2Y2NmI4NTQ4MmVlZTNjYmE2NzdjYmQ1NjVmOGM4ZjUzN2FkNzFlMDNhZTUifQ==');

// '{"iv":"GT4pa07kE+sCDmFCynSayg==","value":"f0CVZFKo+0\\/ngbcAZiEVoQ==","mac":"955d37d3fd06e7c664767f66b85482eee3cba677cbd565f8c8f537ad71e03ae5"}'

[8]  $jsonDecode = json_decode($baseDecoded,true);
// array(
//   'iv' => 'GT4pa07kE+sCDmFCynSayg==',
//   'value' => 'f0CVZFKo+0/ngbcAZiEVoQ==',
//   'mac' => '955d37d3fd06e7c664767f66b85482eee3cba677cbd565f8c8f537ad71e03ae5'
// )
[11]  

You see for a valid base64 encoded value the json_decode results in the above associated array containing the initialization vector (iv) your encrypted value (value) and the Message Authentication Code (mac). If this array is not being generated for your value inside the db then your data is not being save properly inside the database and you might have to look into the schema --see documentation.

In case the data is correct then you might have accidentally changed the secret key -- but that should result in an invalid MAC error.

Usman.

sid405's avatar
Level 27

@usman You certainly seem to know the way round that mechanism :)

Edit:

Here's the record placed on pastebin as it was a little too bigto submit here:

http://pastebin.com/raw.php?i=JuUQsGVU

Thanks again for looking into this, man. Much appreciated,

Sid

usman's avatar

@sid405 this is definitely wrong. Did you extend the database driver?

sid405's avatar
Level 27

@usman That is one record. The record with that key. It's one single query.

And it's being generated with

public function put($key, $value)
     {
        return $this->cache->forever($key, $value);
     }

No more, no less.

Nope didn't extend the driver. Nothing esoteric was done in this particular installation. Spend hours on this yesterday and still could not figure it out. For the moment if've left the cache on file and maybe i'll even consider leaving memcached as a solution if i can't get the database cache driver sorted :/

usman's avatar

@sid405 sorry I have tried some combination to reproduce the error on a default installation but could not reproduce this case. Can you share your migration for cache table?

sid405's avatar
Level 27

Of course. Here it is.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCacheTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cache', function(Blueprint $table)
        {
            $table->string('key')->unique();
            $table->text('value');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('cache');
    }

}

Wait, so for you that record is compromised or not?

sid

usman's avatar
usman
Best Answer
Level 27

@sid405 I think I have found out the problem. It seems to me that your 64 enocded data is being strip down due to column length and I think you are storing something relatively big. I just reproduced your error by adding a very large cache value into the database. Make the following changes inside your schema, rollback and rerun the migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCacheTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cache', function(Blueprint $table)
        {
            $table->string('key')->unique();
            $table->longText('value');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('cache');
    }

}

The record you pasted previously giving me a NULL and I think it is because it is striped down.

After migrating the cache table. Put the same cache value and read back hopefully you will not get error this time. Finger Crossed!

Usman

4 likes
sid405's avatar
Level 27

@usman You, my friend, are a modern day hero.

Thank you infinitely. That was it.

Of course in hindsight, i feel truly a moron for no considering it.

Thank you again, man.

usman's avatar

@sid405 Even if it has solved the issue, Use this migration it adds the expiration column as well:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCacheTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cache', function(Blueprint $table)
        {
            $table->string('key')->unique();
            $table->longText('value');
            $table->integer('expiration');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('cache');
    }

}
1 like
sid405's avatar
Level 27

@usman Yep i actually added that too :) thanks dude. I'll be sure to pick your brain in the next seemingly inexplicable situation i find myself in with Laravel.

Best sid

gaurav16694's avatar

there might be some issue with the .env file as i am having the same problem. there should be space between APP_KEY and APP_DEBUG

Please or to participate in this conversation.