【问题标题】:PHP mcrypt_module_open causes 500 errorPHP mcrypt_module_open 导致 500 错误
【发布时间】:2012-05-22 21:02:32
【问题描述】:

我有这个用于解密文件的 PHP 函数(使用 PHP 5.3),它以前工作得很好,但现在我搬到了 Amazon EC2(基于 Amazon Linux Image 2012.3),似乎 mcrypt install 是要么已损坏,要么根本不可用。

初步测试表明,文件解密确实适用于较小的文件,但不适用于 20MB 以上的文件(不是特别大的文件)。

我将问题追踪到 这一行,这导致了错误 500(我没有得到mcrypt_module_open is undefined只是 500 服务器错误)

$td = mcrypt_module_open ('rijndael-128', '', 'cbc', '');

奇怪的是,我检查了 /etc/php.ini,我根本看不到 mcrypt(当然假设我正在查看正确的 php.ini/path!)

PHP 代码/函数是:

function decrypt_file ($inputfile, $outputfile)
{
    $key        = FILE_KEY;  // <-- assign private key
    $buffersize = 16384;

    // Open $inputfile for reading binary
    $input      = fopen ($inputfile, 'rb');

    // Error opening $inputfile, return false
    if (!$input)
        return false;

    // Open $outputfile for writing binary
    $output     = fopen ($outputfile, 'wb');

    // Error opening $outputfile, return false
    if (!$output)
        return false;

    // Open the cipher module
    $td = mcrypt_module_open ('rijndael-128', '', 'cbc', '');

    // Read the IV from $inputfile
    $iv = fread ($input, 16);

    // Compute the SHA512 of the IV (salt) and Key and use 32 bytes (256 bit) of the result as the encryption key
    $keyhash = substr (hash ('sha512', $iv . $key, true), 0, 32);

    // Intialize encryption
    mcrypt_generic_init ($td, $keyhash, $iv);

    while (!feof ($input))
    {
        $buffer = fread ($input, $buffersize);

        // Encrypt the data
        $buffer = mdecrypt_generic ($td, $buffer);

        // Remove padding for last block
        if (feof ($input))
        {
            $padsize = ord ($buffer[strlen ($buffer) - 1]);
            $buffer  = substr ($buffer, 0, strlen ($buffer) - $padsize);
        }

        // Write the encrypted data to $output
        fwrite ($output, $buffer, strlen ($buffer));
    }

    fclose ($input);
    fclose ($output);

    // Deinitialize encryption module
    mcrypt_generic_deinit ($td);

    // Close encryption module
    mcrypt_module_close ($td);

    return true;
}

有人知道怎么解决吗?我正在使用 PHP 5.3 和 CodeIgniter 2.1(认为这很可能与 CodeIgniter不相关

【问题讨论】:

  • var_dump(function_exists('mcrypt_module_open')); 说什么?上面写着TRUE,错误日志是怎么说的?
  • 不,上面写着bool(false),所以我想我需要安装模块或启用它?
  • 是的,您好像没有安装 mcrypt。尝试从实例的命令行运行 sudo yum install php-mcrypt
  • 戴夫你摇滚!您能否将此作为答案发布,以便我接受?非常感谢! :)

标签: php codeigniter encryption


【解决方案1】:

您似乎没有安装 mcrypt。尝试运行:

sudo yum install php-mcrypt

...从您实例的命令行中。

【讨论】:

  • mac osx (yostemite 最新) 怎么样,也安装了 mcrypt 模块.. 但仍然收到 500 内部服务器错误.. 使用“AES.php”.. 函数 mcrypt_encrypt()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多