PHP mcrypt_module_open causes 500 errors

I have this PHP function (using PHP 5.3) that I use to decrypt files, it works fine, but now that I switched to Amazon EC2 (based on Amazon Linux Image 2012.3), it seems that the mcrypt installation is either corrupt. either not available at all.

Initial tests show that file decryption works with smaller files, but not on 20 MB + files (which is not a particularly large size).

I traced the problem to this line, which causes a 500 error (I do not receive mcrypt_module_open is undefined, only 500 server errors )

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

Which is strange, I checked /etc/php.ini, I donโ€™t see mcrypt at all (assuming I'm looking at the correct php.ini / path, of course!)

PHP code / function:

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)

+3
1

, mcrypt. :

sudo yum install php-mcrypt

... .

+5

All Articles