1

I am trying to use encryption library in Codeigniter 3.1.3 in HMVC provided by Wiredesignz. I even tried initializing the encryption library with my custom settings as below as it didn't work with default:

    $this->load->library('encryption');
    $this->encryption->initialize(
        array(
                'cipher' => 'aes-256',
                'driver' => 'openssl',
                'mode' => 'ctr'
            )
        );
    $this->load->module('site_security');
    $str = "12345";
    $encrypted_str = $this->encryption->encrypt($str);
    echo "encrypted_str: ".$encrypted_str; //works fine gives me encrypted string.
    $decrypted_str = $this->encryption->decrypt($encrypted_str);
    echo "<br>decrypted_str: ".$decrypted_str; // always gives me empty string

I used the encrypt method to successfully encrypt a string but I can't decrypt it, it always gives me empty string. I also set the $config['encryption_key'] to a 32 characters string in config/config.php file.

2
  • Look at this link Commented Jun 28, 2017 at 3:43
  • are you want to descript your key? am I getting it correct? Commented Jun 28, 2017 at 3:55

2 Answers 2

1

Codeigniter by default uses $config['encryption_key'] which you can find it in the config file, for the cryptographic process!

so for decrypting it, you have to first have this key! then you can decrypt it as follows:

$this->load->library('encrypt');

$encrypted_password = 'what you want to descript, put that here';
$key = 'secret-key-in-config';

$decrypted_string = $this->encrypt->decode($encrypted_password, $key);

and after that, you can encrypt it again!

Sign up to request clarification or add additional context in comments.

Comments

0

First set encryption_key in your config file.

$config['encryption_key'] = 'something';

Second set load library in your controller.

public function test()
    {
         $this->load->library('encryption');
         $plain_text = 'This is a plain-text message!';
            $ciphertext = $this->encryption->encrypt($plain_text);

        // Outputs: This is a plain-text message!
        echo $this->encryption->decrypt($ciphertext);
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.