Question

In magento 2.3.1 I have this code:

public function encrypt($params)
    {
        return base64_encode($this->encryptor->encrypt(serialize($params)));
    }

    /**
     * Decrypt tracking key
     *
     * @param string $key
     * @return array
     */
    public function decrypt($key)
    {
        $serializedParams = $this->encryptor->decrypt(base64_decode($key));
        return unserialize($serializedParams);
    }

It is from a module which tracks newsletters. But I get error

Notice: unserialize(): Error at offset 0 of 48 bytes in myfile.php on line 51

Which is

return unserialize($serializedParams);

Can anyone help me with the right syntax of this please?

Was it helpful?

Solution

Please go here and change the code as per the below instruction.

public function encrypt($params)
    {
        return base64_encode($this->encryptor->encrypt(json_encode($params))); 
    }
/**
 * Decrypt tracking key
 *
 * @param string $key
 * @return array
 */
public function decrypt($key)
{
    $serializedParams = $this->encryptor->decrypt(base64_decode($key));
    return json_decode($serializedParams,true); 
}

3. Please run the below command

 php bin/magento c:f
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top