0

I am new in PHP programming and I want to encrypt and decrypt something so for that I am using following code. Error coming at when I try to use mcrypt functions. Can you help me out how I can use the library for this and how to include the related headers? Also, I want to implement related method through FileZilla because at now I have not access on my server.

code is

<?php

error_reporting(0);

function encrypt($plainText,$key)
{
    $secretKey = hextobin(md5($key));
    $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
    $openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');
    $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'cbc');
    $plainPad = pkcs5_pad($plainText, $blockSize);
    if (mcrypt_generic_init($openMode, $secretKey, $initVector) != -1) 
    {
          $encryptedText = mcrypt_generic($openMode, $plainPad);
              mcrypt_generic_deinit($openMode);

    } 
    return bin2hex($encryptedText);
}

function decrypt($encryptedText,$key)
{
    $secretKey = hextobin(md5($key));
    $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
    $encryptedText=hextobin($encryptedText);
    $openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');
    mcrypt_generic_init($openMode, $secretKey, $initVector);
    $decryptedText = mdecrypt_generic($openMode, $encryptedText);
    $decryptedText = rtrim($decryptedText, "\0");
    mcrypt_generic_deinit($openMode);
    echo "text Decrypted";
    return $decryptedText;

}
//*********** Padding Function *********************

 function pkcs5_pad ($plainText, $blockSize)
{
    $pad = $blockSize - (strlen($plainText) % $blockSize);
    return $plainText . str_repeat(chr($pad), $pad);
}

//********** Hexadecimal to Binary function for php 4.0 version ********

function hextobin($hexString) 
 { 
        $length = strlen($hexString); 
        $binString="";   
        $count=0; 
        while($count<$length) 
        {       
            $subString =substr($hexString,$count,2);           
            $packedString = pack("H*",$subString); 
            if ($count==0)
        {
            $binString=$packedString;
        } 

        else 
        {
            $binString.=$packedString;
        } 

        $count+=2; 
        } 
        return $binString; 
      } 
?>

I get the error in this line

$openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');

After of this line I tried to print something but nothing get printed and even my server "saying Internal Server Error 500" because my server didnot included mcrypt library but I just uploaded library but it's not working

Tech Team
  • 109
  • 13
  • Just to give us a starter for 10, would you like to tell us what error you are getting. The complete error including line number if you please. And then tell us which line that is in the code you have shown us. Some people here are Very very clever, but none of them are **clairvoyant** – RiggsFolly Jun 20 '17 at 16:39
  • @RiggsFolly In this line $openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', ''); – Tech Team Jun 20 '17 at 16:44
  • 1
    And now **What is the error message** please. Remember you can add info to your question by using the [edit](https://stackoverflow.com/posts/44658646/edit) link under your question – RiggsFolly Jun 20 '17 at 16:45
  • Oh, a quick look at the manual, I guess you didnt bother with that, shows ___mcrypt_module_open___ **Warning** _This function has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged._ – RiggsFolly Jun 20 '17 at 16:48
  • From [Deprecated features in PHP 7.1.x](http://php.net/manual/en/migration71.deprecated.php): *The mcrypt extension has been abandonware for nearly a decade now, and was also fairly complex to use. It has therefore been deprecated in favour of OpenSSL, where it will be removed from the core and into PECL in PHP 7.2.*. So don't put your eggs in that basket. – Álvaro González Jun 20 '17 at 16:49
  • "500 Internal Server Error" (or a blank page) means your script is throwing an error but PHP is configured to hide it from you. You need to fix it ASAP because coding without the aid of error messages is hard. As quick start, you can set the `error_reporting` and `display_errors` directives in your computer's system-wide `php.ini` file ([details here](http://stackoverflow.com/a/5680885/13508)). Errors thumb rule: show in development, log in production. – Álvaro González Jun 20 '17 at 16:50
  • @ÁlvaroGonzález Please can you tell me that If I am still going with this method then How I include header files for mcrypt, it will be really helpful – Tech Team Jun 20 '17 at 16:51
  • You cannot use FTP to install software, if that's what you mean, unless your hosting provider offers free access to the complete server disk (which I hope they don't). You'll need to inspect the installed extensions and see of OpenSSL is available, or find a pure PHP implementation. – Álvaro González Jun 20 '17 at 16:55

0 Answers0