2

Hello I am using 2 functions for encryption and decryption

my Decryption function is as follow

  function decrypt($string, $key = NULL) {
   // $key = 'this is new thign gor security';
    $string = base64_decode(base64_decode($string));
    $key = md5($key); //to improve variance
    /* Open module, and create IV */
    $td = mcrypt_module_open('des', '', 'cfb', '');

    $key = substr($key, 0, mcrypt_enc_get_key_size($td));
    $iv_size = mcrypt_enc_get_iv_size($td);
    $iv = substr($string, 0, $iv_size);
    $string = substr($string, $iv_size);
    /* Initialize encryption handle */
    if (mcrypt_generic_init($td, $key, $iv) != -1) {
      /* Encrypt data */
      $c_t = mdecrypt_generic($td, $string);
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
      return $c_t;
    } //end if
  }

but in this function if I pass string which is not actually Encryption then it is giving me Notice undefined variable and does not give any output so thats why I add try catch blog in to it as bellow

function decrypt($string, $key = NULL)
{


    try
    {
        $string = base64_decode(base64_decode($string));
        $key = md5($key); //to improve variance
        /* Open module, and create IV */
        $td = mcrypt_module_open('des', '', 'cfb', '');

        $key = substr($key, 0, mcrypt_enc_get_key_size($td));
        $iv_size = mcrypt_enc_get_iv_size($td);
        $iv = substr($string, 0, $iv_size);
        $string = substr($string, $iv_size);
        /* Initialize encryption handle */
        if (mcrypt_generic_init($td, $key, $iv) != -1)
        {
            /* Encrypt data */
            $c_t = mdecrypt_generic($td, $string);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            return $c_t;
        } //end if
    } catch (Exception $exc)
    {
        echo $exc->getTraceAsString();
    }


    // $key = 'this is new thign gor security';
}  

BUT again it struct at same point and does not go to catch

Here I am putting some string and Key

encrypted string: dExXZStvRmV6WFR5NkE9PQ==

and KEY is : !1@2#3$4%5^6&7*8(9)0_-+=

You can use this function Like this

$xyz = decrypt('dExXZStvRmV6WFR5NkE9PQ==','!1@2#3$4%5^6&7*8(9)0_-+=');

enter image description here

Chintan Gor
  • 1,062
  • 2
  • 15
  • 35
  • what line is it giving for the undefined variable error, also note by default you cannot catch notices/warnings you need to use a custom handler: http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning – Patrick Evans Feb 14 '14 at 05:06
  • I upload picture of my notice – Chintan Gor Feb 14 '14 at 05:09
  • i dont see `Notice undefined variable` in there... but i see a warning telling you that you are passing an empty string, so you should check for empty strings before passing them to the function. – Patrick Evans Feb 14 '14 at 05:12
  • @PatrickEvans when I am running this function and when I passed string which was not encrypted by my function then it will give me error $c_t = mdecrypt_generic($td, $string); at this like because in this case $string will be empty – Chintan Gor Feb 14 '14 at 05:12
  • yes sir that is one solution but I wonderd because it is not catching this exception in TRY catch section – Chintan Gor Feb 14 '14 at 05:13
  • 1
    because as i mentioned earlier warnings/notices do not trigger catches you have to use a custom handler. – Patrick Evans Feb 14 '14 at 05:14
  • A warning is not an exception. – Burhan Khalid Feb 14 '14 at 05:15
  • Okey Got it, you mean if I want to catch warning in catch then I need to throw manually exception when string will return empty – Chintan Gor Feb 14 '14 at 05:17
  • Yes, or check the link in the first comment that shows how to add a custom handler so that it will throw it automatically. – Patrick Evans Feb 14 '14 at 05:21
  • @PatrickEvans Okey sir, I am sorry I did not know that thing I am new to try catch that's why directly ask this question without doing more R&D Thank you – Chintan Gor Feb 14 '14 at 05:23

1 Answers1

0

try to throw an exception and see what is the issue with your code, then you can resolve your problem.

try this inside your condition: print_r(error_get_last());