I got a piece of code from Internet to encrypt my data using TripleDES.
$key = "ThisIsTheKey"; // 24 bit Key
$iv = "fYfhHeDm"; // 8 bit IV
$bit_check = 8; // bit amount for diff algor.
//function to encrypt
function encrypt($text) {
global $key,$iv,$bit_check;
$text_num = str_split($text, $bit_check);
$text_num = $bit_check - strlen($text_num[count($text_num) - 1]);
for ($i = 0; $i < $text_num; $i++) {
$text = $text . chr($text_num);
}
$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mcrypt_generic($cipher, $text);
mcrypt_generic_deinit($cipher);
return base64_encode($decrypted);
}
The problem is even though I call the variables as global, in the top (where I declared the variables) show the variable are unused. As well as when I try to run this it gave an error. But when I declare the same variable set inside the function it works.
As a general note, The use of global variables is highly not recommended. When I look at your function, I see it only needs
$text, however, it actually needs$text,$key,$iv, and$bit_check.Lets try not using globals:
And call it using
Another solution involves the use of CONSTANTS, assuming the key, iv and bit_check will never change throughout the entire execution time, you can define them as constants, and they would be globally available throughout the entire application, and will not be able to change.
Like so: