mixkat
mixkat

Reputation: 3785

mcrypt_encrypt fails to initialise

I am trying to encrypt some data in PHP using the Rijndael cipher in CBC mode with a 256bit key but for some reason I get the following error message:

mcrypt_encrypt() Module initialization failed

My code:

    $hashKey = hash('sha256',$key);
    $iv = hash('sha256',$hashKey);

    //                                                 ------Cipher-------------key-------------Data-------------Mode---------IV--
    $encryptedQuestion = base64_encode(mcrypt_encrypt('MCRYPT_RIJNDAEL_256', $hashKey , $_POST['question'], MCRYPT_MODE_CBC, $iv));

Can anyone see whats wrong with this?

Upvotes: 4

Views: 5092

Answers (3)

Pat
Pat

Reputation: 1

In php 5.6 try mcrypt_ecb to overcome the invalid key issue

    $choice ="2";
      $key = "1234";
$key = hash("sha512", $key, TRUE);

    for ($x = 0; $x < 8; $x++) {
        $key = $key . substr($key, $x, 1);
    }
    $msg = "pato";
    echo("key is".$key." \n");

    if ($msg == ''){
        die("Please enter a text to encrypt! ");
        }
    if ($key == ''){
        die("Please enter a key! ");
        }        


   function pkcs5_pad($text, $blocksize) {

    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
    }
 function pkcs_pad($text, $blocksize) {

    //$pad = $blocksize - (strlen($text) % $blocksize);
        $tes=substr($text,0,$blocksize) ;
    return $tes;
    }
    function encryptnow( $thekey, $themsg)
    {



$padded = pkcs5_pad($themsg, mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
           echo("padded".$padded."\n");
  $keypad = pkcs_pad($thekey, mcrypt_get_key_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
   $i;
  //$iv=pkcs5_pad($i, mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
   //$iv = mcrypt_create_iv(8, MCRYPT_DEV_RANDOM);
         //  echo("padded".$keypad."\n");
    $encrypted = base64_encode(mcrypt_ecb(MCRYPT_3DES, $keypad, $padded, MCRYPT_MODE_CBC));


 echo "<html><hr size='2' ></html>";
        echo "<P><P><b>Plain Text : </b>";
        echo($themsg);
        echo "<p><b>Cipher Text : </b> ";
        echo "$encrypted";

        die();
    }


   if ($choice == '2'){



        encryptnow($key, $msg);
   }

Upvotes: 0

edocabhi
edocabhi

Reputation: 137

I got a similar error. I assigned the constants to a variable and passed the variable and that was the cause of the error.

This failed for me > return new encKey($cipher_name, $mode, $value);

Now this works > return new encKey(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB, $value);

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173522

There are a few issues with the code that I can spot:

  1. Your $iv should not be dependent on $hashKey; rather, you should create it separately using mcrypt_create_iv().

  2. Your $hashKey should be binary rather than textual.

  3. MCRYPT_RIJNDAEL_256 is a constant, it should not be passed as a string.

The following code is more verbose than yours, but it should give you an insight in the steps required to encrypt something:

$crypto = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($crypto), MCRYPT_DEV_URANDOM);

$hkey = hash('sha256', $key, true);

mcrypt_generic_init($handle, $hkey, $iv);

$enc_question = mcrypt_generic($handle, $_POST['question']);

mcrypt_generic_deinit($handle);
mcrypt_module_close($handle);

I've also left out any error checks.

Upvotes: 5

Related Questions