Welcome Guest, Not a member yet? Register   Sign In
Decrypting: authentication error when using the encryption library
#1
Smile 

Hello friends,

I am currently trying to use the encryption library on Codeigniter 4 (I am not using it for passwords Smile  ). When i use the $this->encrypter->encrypt($var) , it seems to work, cus I don't get any errors and I get what seems like gibberish in my database as I would want to. 

However , when I fetch the encrypted data and try to decrypt I get this error :  "Decrypting: authentication error" . Below are my codes.

Encryption config file

Code:
public $key = 'secret key';


Constructor to initialize service


Code:
public function __construct()
  {
$this->encrypter = \Config\Services::encrypter();

}

Decrypting: authentication failedDecrypting: authentication failed
Encrypting data
Code:
$this->encrypter->encrypt($var)

Decrypting: authentication failed
Decrypt data

I tried both

Code:
<?=$encrypter->decrypt($var);?>

and


Code:
<?=$encrypter($var,$key);?>


Yet I was still getting errors, Any help is appreciated.

Regards. 
Decrypting: authentication failedDecrypting: authentication failed
Reply
#2

You should use base64_encode() and base64_decode() methods.

PHP Code:
$this->encrypter->encrypt(base64_encode($var));

$encrypter->decrypt(base64_decode($var)); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 08-12-2020, 03:41 AM by ebuoe.)

(08-11-2020, 04:03 AM)InsiteFX Wrote: You should use base64_encode() and base64_decode() methods.

PHP Code:
$this->encrypter->encrypt(base64_encode($var));

$encrypter->decrypt(base64_decode($var)); 

Hello InsiteFx ,

I have tried this method and i'm still getting the same Decrypting: authentication failed. error.

Regards.

Hello Everyone I was able to solve the problem, I was loading the encryption service without loading the and passing the default configuration first, so instead of :


Code:
public function __construct(){

  $this->encrypter = \Config\Services::encrypter();

}


I did this :

Code:
public function __construct(){

  $this->config    = new \Config\Encryption();      // load the configuration for the encryption service
  $this->encrypter = \Config\Services::encrypter($this->config); // start the encryption service
}
Reply
#4

Just to add , using base64_encode and base64_decode does not work for me
Reply




Theme © iAndrew 2016 - Forum software by © MyBB