0

I found a tutorial online on how to encrypt strings in php but when I call the function and try echo the processed data I'm getting 500 internal error. Here is my code below.

 <?php



    $iv_to_pass_to_decryption = 'mysecretpass';
    function encrypt($text, $key)
    {
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND); 
        $iv_to_pass_to_decryption = base64_encode($iv);
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv); 
    }




    function decrypt($text, $key, $iv)
    { 
        $text = base64_decode($text);
        $iv = base64_decode($iv);
        return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
    }


    $txt = "hello";
    $mykey = "mysecretkey";
    $somedata = encrypt($txt, $mykey);

    echo $somedata;




    ?>
Offset
  • 53
  • 1
  • 8
  • what's your php version? [mcrybt_*](http://php.net/manual/en/intro.mcrypt.php) extension had been deprecated; – hassan Apr 23 '17 at 13:12
  • @hassan I'm using php7 do have alternative to this? – Offset Apr 23 '17 at 13:13
  • I suggest you to use openssl to encrypt in php. Here one example : http://blog.turret.io/the-missing-php-aes-encryption-example/ More examples with google – MTK Apr 23 '17 at 13:15
  • Possible duplicate of [How do you Encrypt and Decrypt a PHP String?](http://stackoverflow.com/questions/16600708/how-do-you-encrypt-and-decrypt-a-php-string) – Peter Apr 23 '17 at 13:27

2 Answers2

0

The first problem is, you missed a ) in line 8.

The second problem is mcrypt_decrypt()function is deprecated.

The third problem is mcrypt_encrypt(): Key of size 11 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported. The 'mysecretkey' key is wrong.

I can recommend use the crypt() function: http://php.net/manual/en/function.crypt.php

When validating passwords, a string comparison function that isn't vulnerable to timing attacks should be used to compare the output of crypt() to the previously known hash. PHP 5.6 onwards provides hash_equals() for this purpose.

Peter
  • 748
  • 6
  • 20
-2

use below code hope it will help you

    $iv_to_pass_to_decryption = 'mysecretpass';
    function encrypt($text, $key)
    {
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND); 
        $iv_to_pass_to_decryption = base64_encode($iv);
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv));
    }




    function decrypt($text, $key, $iv)
    { 
        $text = base64_decode($text);
        $iv = base64_decode($iv);
        return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
    }


    $txt = "hello";
    $mykey = "mysecretkey12345";
    $somedata = encrypt($txt, $mykey);

    echo $somedata;
Amit Gaud
  • 756
  • 6
  • 15
  • mcrypt_create_iv,mcrypt_decrypt functions are deprecated in PHP7, the user uses PHP7. – Peter Apr 23 '17 at 13:19
  • Hey Dear I am using PHP7 it is working fine it is derpecate from PHP 7.1.0 – Amit Gaud Apr 23 '17 at 13:23
  • 1
    @AmitGaud , it will be totally deprecated in php 7.1, however you need to know why it had been deprecated to stop using it even if you use php5 – hassan Apr 23 '17 at 13:24
  • Yes, and we don't recommend deprecated functions and solutions here. What will happen if user will upgrade php version? Please. .. – Peter Apr 23 '17 at 13:25
  • If you are using PHP version less than 7.1.0 then it will run like charm if you are upgrading to it to 7.1.0 i will suggest use crypt() function. – Amit Gaud Apr 23 '17 at 13:47