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)
var_dump(function_exists('mcrypt_module_open'));
say? It it saysTRUE
, what does the error log say?bool(false)
, so I guess I need to install the module or enable it?sudo yum install php-mcrypt
from the command line on your instance.