CI 3 to CI 4 Encryption Compability

Viewed 58

I have 1 app for SSO, use CI 3. Now i am creating new app with CI 4. code in CI 3 for encryption :

$otp = 123;
$this->encryption->initialize(array(    
    'cipher' => 'aes-256',
    'mode' => 'ctr',
    'key' => '12345678901234567890123456789012'
));
echo $this->encryption->encrypt($otp);
//result 54322c75199be967a78c4d65906dd1d6a41c54027ddb2ff1a295c5646728f435cf4c3b5e793b57b6a6ecef34de7b7c1ed809632adeb02c5fabdf5f76befe4440PFRX6XAktE/jZXJKjL8fhCcqQw==

code in CI 4 for decryption :

$otp = '54322c75199be967a78c4d65906dd1d6a41c54027ddb2ff1a295c5646728f435cf4c3b5e793b57b6a6ecef34de7b7c1ed809632adeb02c5fabdf5f76befe4440PFRX6XAktE/jZXJKjL8fhCcqQw==';

$config         = new \Config\Encryption();
$config->key    = '12345678901234567890123456789012';
$config->driver = 'OpenSSL';
$encrypter = \Config\Services::encrypter($config);
echo $encrypter->decrypt($otp);

//result CodeIgniter\Encryption\Exceptions\EncryptionException Decrypting: authentication failed.

i have try with different option :

$config         = new \Config\Encryption();
$config->key    = '12345678901234567890123456789012';
$config->driver = 'OpenSSL';
$config->digest = 'sha256' or 'sha512';

Still getting message authentication failed. but in this documentation states that my CI 3 configuration it same with CI 4 even with default.

1 Answers

Since no one seem interesting to the subject. i have figure it out hot to resolve this problem. so. what i am doing is. take / copy Codeigniter 3 encryption class from system/libraries/Encryption.php and make it third party Library in Codeigniter 4 and put it in app/libraries/Ci3encrypt.php (i rename it to this because i'm afraid the class name be the same with ci4 class)

<?php

namespace App\Libraries;

class Ci3encrypt
{
//and the rest is still the same

to call new class. in CI4 on the controller i put

namespace App\Controllers;
use App\Libraries\Ci3encrypt;

and on the function in controller ci4

$ci3 = new Ci3encrypt();
$ci3->initialize(array( 
    'cipher' => 'aes-256',
    'mode' => 'ctr',
    'driver' => 'openssl',
    'key' => '12345678901234567890123456789012'
));
$plain_text=$ci3->decrypt($ciphertext);

and my problem is resolve. finally

for more information visit :

https://github.com/codeigniter4/CodeIgniter4/pull/6277 https://forum.codeigniter.com/showthread.php?tid=82494

Related