3

I have this PHP function (using PHP 5.3) that I use to decrypt files, it used to work just fine, but now that I moved to Amazon EC2 (based on Amazon Linux Image 2012.3), it seems that mcrypt install is either corrupted or not available at all.

Initial tests suggests that file decryption does work on smaller files, but not on 20MB+ files (which is not a particularly large size).

I tracked the problem down to this line, which is causing an Error 500 (I'm not getting mcrypt_module_open is undefined, just 500 server error)

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

What's strange, I checked /etc/php.ini, I can't see mcrypt at all (assuming I'm looking at the correct php.ini/path of course!)

The PHP code/function is:

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;
}

Anyone knows how to fix that? I'm using PHP 5.3 with CodeIgniter 2.1 (thought this is most likely not related to CodeIgniter)

4
  • 1
    What does var_dump(function_exists('mcrypt_module_open')); say? It it says TRUE, what does the error log say? Commented May 15, 2012 at 11:10
  • No, it says bool(false), so I guess I need to install the module or enable it? Commented May 15, 2012 at 11:19
  • 1
    Yep, looks like you don't have mcrypt installed. Try running sudo yum install php-mcrypt from the command line on your instance. Commented May 15, 2012 at 11:22
  • Dave you rock! Can you please post this as an answer so that I can accept it? Thanks a lot! :) Commented May 15, 2012 at 11:29

1 Answer 1

5

It looks like you don't have mcrypt installed. Try running:

sudo yum install php-mcrypt

...from the command line on your instance.

Sign up to request clarification or add additional context in comments.

1 Comment

what about mac osx (yostemite latest), installed mcrypt module as well.. but still getting 500 internal server error.. using "AES.php".. function mcrypt_encrypt()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.