Got an error "Decrypting: authentication failed" while using Encryption service |
Hi, in my project I need to store category id in category edit page inside a hidden field. Then, after form submit I want to update that particular category. To achieve this, in a secured approach, I used Encryption service of CodeIgniter 4.
Note: In the App\Config\Encryption.php file, I already set the "base 64" encoded value for "key" property that I got after executing createKey() static function of the Encryption class. Also, I included that Encryption class with its namespace that is "CodeIgniter\Encryption\Encryption". Below code is for the editCategory function, where I create the encryption key, then using that key I encrypt category id. Finally, I pass that to the "edit_category" view file. PHP Code: public function editCategory($categoryId) { In edit_category.php file, I pass the encrypted category id to the updateCategory function. Code snippet for hidden field in edit_category.php is written below. PHP Code: <input type="hidden" name="categoryId" value="<?= isset($categoryId) ? $categoryId : '' ?>"> Codes for updateCategory function is shown below, in that function I wanted to display the decrypted version of the category id. PHP Code: public function updateCategory() { Have I done any mistake? Please help.
In your editCategory function, you setup two instances of the encryption config.
$config = config('Encryption'); // the shared instance $configuration = new \Config\Encryption(); // the other instance Yet, you passed to the encrypter is the "other" instance. This causes the wrong starter key to be used in your decryption. You need to manipulate the shared instance. Then in both editCategory and updateCategory, you don't really need two instances of the same config file. Just use the shared config instance across your code.
(09-25-2020, 09:19 AM)paulbalandan Wrote: In your editCategory function, you setup two instances of the encryption config. Thanks for your advice. |
Welcome Guest, Not a member yet? Register Sign In |