Will a encrypted password using codeigniter's encryption class produce the same encrypted password on two different machines? For example, I have a production site online with some host and i have the local install on my computer. But the passwords don't match when i restore the database from one to the other.
问题:
回答1:
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.
回答2:
I was looking for an answer to this but I figured out the solution, it maybe vague but should be simple enough for novices:
Install libmcrypt and install php-mcrypt.
Edit your php.ini file. Some distros may differ, mine is located at: /etc/php/php.ini
Find where all the extensions are and add the uncomment or add the following to your php.ini file.
extension=mcrypt.so
Restart apache or whatever webserver you use.
回答3:
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";
回答4:
Use this in the config.php file. It will give you a different key but the result will be the same:
$config['encryption_key'] = "YOUR KEY";