PHP MCRYPT不断抛出模块初始化失败警告

时间:2012-12-22 19:04:58

标签: php encryption mcrypt

每次我尝试运行m_decrypt时,都会抛出以下错误:

Warning: mcrypt_get_key_size(): Module initialization failed in /var/www/milo/system/encryption/common.php on line 51 Warning: mcrypt_get_block_size(): Module initialization failed in /var/www/milo/system/encryption/common.php on line 54 Warning: mcrypt_decrypt(): Module initialization failed in /var/www/milo/system/encryption/common.php on line 55  

以下是驱动所有内容的代码:

class encrpt
{
    protected $data;
    protected $key = "JUST A KEY";
    protected $cipher = "MCRYPT_SERPENT_256";
    protected $mode = "MCRYPT_MODE_CBC";

    public function m_encrypt($data)
    {
        return (string) 
         base64_encode(
          mcrypt_encrypt(
           $this->cipher,
           substr(md5($this->key),0,mcrypt_get_key_size($this->cipher, $this->mode)),
           $data,
           $this->mode,
           substr(md5($this->key),0,mcrypt_get_block_size($this->cipher, $this->mode))
          )
         );
    }

    public function m_decrypt($data)
    {
        return (string)
          mcrypt_decrypt(
           $this->cipher,
           substr(md5($this->key),0,mcrypt_get_key_size($this->cipher, $this->mode)),
           base64_decode($data),
           $this->mode,
           substr(md5($this->key),0,mcrypt_get_block_size($this->cipher, $this->mode))
          );
    }
}

我不知道我错过了什么。我的php-mcrypt模块是否已损坏或缺少依赖项?我在PHP 5.3上运行

2 个答案:

答案 0 :(得分:8)

好的解决了。我把常数写错了。我改变了我的班级变量:

protected $cipher = "rijndael-256";
protected $mode = "cbc";

希望这可以帮助人们不要抨击他们的大脑!

答案 1 :(得分:8)

protected $cipher = MCRYPT_SERPENT_256;
protected $mode = MCRYPT_MODE_CBC;

这些是常量 - 不要使用引号。