0

I keep getting these warnings:

Warning: mcrypt_generic_init(): mcrypt_generic_init(): 1 is not a valid MCrypt resource in C:\Users\dever\Desktop\web code\HeloWorld\HeloWorld\Crypt.php on line 123, column 9.

Warning: mdecrypt_generic(): mdecrypt_generic(): 1 is not a valid MCrypt resource in C:\Users\dever\Desktop\web code\HeloWorld\HeloWorld\Crypt.php on line 124, column 9.

Warning: mcrypt_generic_deinit(): mcrypt_generic_deinit(): 1 is not a valid MCrypt resource in C:\Users\dever\Desktop\web code\HeloWorld\HeloWorld\Crypt.php on line 125, column 9.

Warning: mcrypt_generic_init(): mcrypt_generic_init(): 1 is not a valid MCrypt resource in C:\Users\dever\Desktop\web code\HeloWorld\HeloWorld\Crypt.php on line 100, column 9.

Warning: mcrypt_generic(): mcrypt_generic(): 1 is not a valid MCrypt resource in C:\Users\dever\Desktop\web code\HeloWorld\HeloWorld\Crypt.php on line 105, column 9.

Warning: mcrypt_generic_deinit(): mcrypt_generic_deinit(): 1 is not a valid MCrypt resource in C:\Users\dever\Desktop\web code\HeloWorld\HeloWorld\Crypt.php on line 107, column 9.

Since it is a warning and the strings "seem" to be encrypted I have ignored it till now. However it seems that the remote server I am working with is not liking the encrypted string and returning errors to me stating as much.

Here is the code generating those warnings:

    mcrypt_generic_init($this->_td, $this->_key, $iv);
    $data = mdecrypt_generic($this->_td, $data);
    mcrypt_generic_deinit($this->_td);

and

$iv = mcrypt_create_iv(self::AES_BLOCK_SIZE, $random_source);
    $s = mcrypt_generic_init($this->_td, $this->_key, $iv);
    if( ($s < 0) || ($s === false))
        die( "Really an error" );
    $data = mcrypt_generic($this->_td, $data);
    $data = $iv.$data;
    mcrypt_generic_deinit($this->_td);

I have researched but could not find out what these warnings meen, if they are effecting the encryption/decryption or what exactly is the problem? Im pretty sure its not the code (as I have it working on a different server).

owen gerig
  • 6,165
  • 6
  • 52
  • 91
  • Apparently, `$this->_td` is not a valid MCrypt resource, exactly as the error message is telling you. You will have to show how you initialize `$this->_td`. – lanzz Oct 25 '12 at 18:01
  • $this->_td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', ''); So the 1 stands for first parameter? – owen gerig Oct 25 '12 at 18:03
  • No, the `1` is what is getting passed in via the `$this->_td` variable. – WWW Oct 25 '12 at 18:05
  • Add a `var_dump($this->_td)` after your `mcrypt_module_open`; it should print something like `Resource id #15`, or `bool(false)` if `mcrypt_module_open` failed. _IF_ you get a resource id, add another `var_dump($this->_td)` right before you call `mcrypt_generic_init($this->_td, ...` and check if it shows the same resource id. If it is different, you're overwriting your `$this->_td` somewhere with garbage. – lanzz Oct 25 '12 at 18:07
  • resource(3) of type (mcrypt) - after open | resource(3) of type (Unknown) - before init call. So I guess it is an overwrite but i dont see $this->_td = anywhere other then in the constructor with the call to mcrypt_module_open – owen gerig Oct 25 '12 at 18:11
  • @lanzz please post this as an answer and i will accept it. If I reinitialize the _td variable prior to the calls shown above everything works. So your comments are the problem. just need to find out why or what is changing that variable :( – owen gerig Oct 25 '12 at 18:34

1 Answers1

1

Here is a test I've performed, that reproduces the values you're seeing in your var_dumps:

$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '/usr/lib/mcrypt-modes');
var_dump($td);
mcrypt_module_close($td);
var_dump($td);

It prints:

resource(4) of type (mcrypt)
resource(4) of type (Unknown)

Since you're seeing the same behavior (resource id remains the same, but loses its mcrypt association), it seems that you're closing your module resource at some point between initializing $this->_td and actually calling mcrypt_generic_init.

lanzz
  • 42,060
  • 10
  • 89
  • 98