Welcome Guest, Not a member yet? Register   Sign In
CI 3 to CI 4 Encryption Compability
#1

(This post was last modified: 07-17-2022, 11:10 AM by ardimardiana.)

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

Code:
$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 :

Code:
$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 :

Code:
$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.

i noticed some different between CI3 and CI4 on Encryption Lib. There is something about BASE64 decode and encode in CI3 for encrypt or decrypt.
i have red in this Github Issue, something aboout bin2hex and hex2bin. and try it at my decrypt like : 


Code:
echo $this->encryption->encrypt(base64_decode($otp));
echo $this->encryption->encrypt(hex2bin($otp));

but still getting authentication failed.

note:
i cant rewrite my CI3 SSO because it has many another app using CI3.
Reply
#2

(This post was last modified: 07-18-2022, 05:34 AM by kenjis.)

Unfortunately there seems no compatibility between CI3 and CI4 Encryption.

The raw_data option in CI3 is missing in CI4.
https://www.codeigniter.com/userguide3/l...parameters
Reply
#3

I sent a PR.
Try this code if you can.
https://github.com/codeigniter4/CodeIgni...dee265R154
Reply
#4

The PR https://github.com/codeigniter4/CodeIgniter4/pull/6277 was merged into 4.3 branch.
The enhancement will be included in v4.3.0.
Reply
#5

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


Code:
<?php

namespace App\Libraries;

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

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


PHP Code:
namespace App\Controllers;
use 
App\Libraries\Ci3encrypt

and on the function in controller ci4


PHP Code:
$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 for now. but if in CI 4.3 will be supported. i will be glad
nothing and everything is possimpible
Reply
#6

(This post was last modified: 02-18-2023, 01:51 PM by objecttothis. Edit Reason: clarity )

(07-24-2022, 04:54 AM)ardimardiana Wrote: i have figure it out hot to resolve this problem. so. what i am doing is. take / copy Codeigniter 3 encryption class

@ardimardiana

I'm trying to do the same thing you've described here, but the CI3 Encryption class has a number of dependencies on functions in the CI3 \system\core\common.php library that don't exist in CI4 in the same way... Are there other files you brought over as well? It seems that copy over and rename isn't doing it.

Nevermind... your instructions are missing a key change though.  In the constructor you need to change
PHP Code:
if ( ! isset($this->_key) && self::strlen($key config_item('encryption_key')) > 0

to
PHP Code:
if ( ! isset($this->_key) && self::strlen($key config('Encryption')->key) > 0

Then it will work.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB