codeigniter encryption on production server and local server codeigniter encryption on production server and local server codeigniter codeigniter

codeigniter encryption on production server and local server


I know this is an old question, but I had the same problem and figured it out..

CodeIgniter's encryption library behaves differently in different environments. Specifically, the library auto-detects if you have the PHP mcrypt extension installed, and if so, uses a completely different algorithm than if not.

You probably have mcrypt installed on your server and not in your dev environment or vice versa.

There are two ways to fix this. Either way, you need to extend the built-in encryption class by creating a MY_Encrypt.php class:

Option One: Always use mcrypt, and fail loudly if it is not installed:

class MY_Encrypt extends CI_Encrypt{    public function __construct()    {        if ( ! function_exists('mcrypt_encrypt')) {            throw new Exception("Encryption requires mcrypt PHP extension!");        }        parent::__construct();    }}

Option Two: Never use mcrypt, even if it is installed:

class MY_Encrypt extends CI_Encrypt{    public function __construct()    {        parent::__construct();        //Pretend Mcrypt doesn't exist no matter what        $this->_mcrypt_exists = FALSE;    }}

This will cause CodeIgniter encryption to behave the same way in every environment.

IMHO, the encrypt library should never silently change the algorithm used for encryption based on environment. Automagic encryption algorithm changes are a terrible idea.


I was looking for an answer to this but I figured out the solution, it maybe vague but should be simple enough for novices:

  1. Install libmcrypt and install php-mcrypt.

  2. Edit your php.ini file. Some distros may differ, mine is located at: /etc/php/php.ini

  3. Find where all the extensions are and add the uncomment or add the following to your php.ini file.

    extension=mcrypt.so

  4. Restart apache or whatever webserver you use.


Without seeing your code, it's impossible to tell what the problem may be (this is why you have no answers yet). However, if you're encrypting the string in the same way, as long as you're using the same encryption key, it should be the same. In CI's encryption class (see here), you can do this in the config.php file like this:

$config['encryption_key'] = "YOUR KEY";