Wednesday, 11 October 2017

php - try catch is not giving output as expected

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_-+=');



src="https://i.stack.imgur.com/zYxfC.jpg" alt="enter image description
here">

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...