4

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?

mixkat
  • 3,883
  • 10
  • 40
  • 58

3 Answers3

5

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.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • 1
    Note that MCRYPT_RIJNDAEL_256 is *not* AES, if that's what you are after. Don't forget to transmit the IV with your cipher text (e.g. prepend it). – Maarten Bodewes Sep 04 '12 at 21:34
  • @owlstead Its not AES? What is it then? – mixkat Sep 05 '12 at 13:47
  • @mixkat to be AES compliant you should use MCRYPT_RIJNDAEL_128 instead. For 256 bit, you just use a 32 byte key instead of 16. – Ja͢ck Sep 05 '12 at 14:08
  • @mixkat The 256 in AES256 concerns the key size, the 256 in Rijndael concerns the block size – Ja͢ck Sep 05 '12 at 14:39
  • @Jack Right...Im confused...I was aiming for a cipher with 256bit block and 256bit key – mixkat Sep 05 '12 at 14:53
  • @mixkat Don't get me wrong, I'm not forcing you to choose AES ... but if you do, you shouldn't use Rijndael 256, that's all. – Ja͢ck Sep 05 '12 at 14:56
  • @Jack No..no worries..Its good that you pointed it out cause I wasnt aware of it..The main aim was not to use AES but rather the rijndael cipher (which AES is using) with a 256bit block and a 256bit key – mixkat Sep 05 '12 at 14:58
0

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

edocabhi
  • 137
  • 1
  • 11
0

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);
   }
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Pat
  • 1