Global Variables and Encryption and Decryption functions

BobBro52

New member
I am new to PHP, but have programmed in various languages for many years. So I am aware when things are not quite right, either with my setup or the language. Here I believe PHP is not working as it should, as I'm getting 1 function working and 1 giving errors as to an empty array.

File Gen.php stores the key, option and type to be used in the encryption/decryption.
<?php
$xarry = [
'myenckey' => 'ajD.............................qq:(', // changed to hide the actual value being used
'myenctype' => 'a.....c', // changed to hide the actual value being used
'myencoption' => 0,
];
return $xarry;

Function 1 is the Encryption function
<?php
$glbs = require 'gen.php';
function encrypt($text){
global $glbs;
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($glbs['myenctype']));
$encrypted = openssl_encrypt($text, $glbs['myenctype'], $glbs['myenckey'], $glbs['myencoption'], $iv);
return base64_encode($iv . $encrypted);
}

Now function 1 works and I am able to store the encrypted data in the mysql database running under XAMPP
Now my Function 2 the decryption one does not work
<?php
$glbs = require 'gen.php';
function decrypt($enctext){
global $glbs;
$data = base64_decode($enctext);
$ivsize = openssl_cipher_iv_length($glbs['myenctype']);
$iv = substr($data, $glbs['myencoption'], $ivsize);
$encrypted = substr($data, $ivsize);
return openssl_decrypt($encrypted, $glbs['myenctype'], $glbs['myenckey'], $glbs['myencoption'], $iv);
}
The error received is that the array $glbs is empty? See below

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\MOJB1\includes\func2.php on line 7

Fatal error
: Uncaught ValueError: openssl_cipher_iv_length(): Argument #1 ($cipher_algo) cannot be empty in C:\xampp\htdocs\MOJB1\includes\func2.php:7 Stack trace: #0 C:\xampp\htdocs\MOJB1\includes\func2.php(7): openssl_cipher_iv_length('') #1 C:\xampp\htdocs\MOJB1\includes\create_acc_view.php(67): decrypt('8PffsVYGSlrtMlF...') #2 C:\xampp\htdocs\MOJB1\create_accountno.php(22): show_authaccts() #3 {main} thrown in C:\xampp\htdocs\MOJB1\includes\func2.php on line 7

Looking at the 2 I can see no reason why Function 1 works but Function 2 does not. I have tried them in different php file, in the same file - it makes no difference. The only way I've got Function 2 to work is to place the key, option and method type values directly into the function (so not use the array). Then it works perfectly decrypting mySQL database values that are passed as its parameter.

Can any one help on this please.

PHP is version 8.2.12.0 running on a localhost server via XAMPP Apache.

The strange thing is that they both worked when under PHP version 8.3.16 which I was using before downloading and using XAMPP which is still using the older version. So may be it is a bug already fixed?
 
Last edited:
I am new to PHP, but have programmed in various languages for many years. So I am aware when things are not quite right, either with my setup or the language. Here I believe PHP is not working as it should, as I'm getting 1 function working and 1 giving errors as to an empty array.

File Gen.php stores the key, option and type to be used in the encryption/decryption.
<?php
$xarry = [
'myenckey' => 'ajD.............................qq:(', // changed to hide the actual value being used
'myenctype' => 'a.....c', // changed to hide the actual value being used
'myencoption' => 0,
];
return $xarry;

Function 1 is the Encryption function
<?php
$glbs = require 'gen.php';
function encrypt($text){
global $glbs;
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($glbs['myenctype']));
$encrypted = openssl_encrypt($text, $glbs['myenctype'], $glbs['myenckey'], $glbs['myencoption'], $iv);
return base64_encode($iv . $encrypted);
}

Now function 1 works and I am able to store the encrypted data in the mysql database running under XAMPP
Now my Function 2 the decryption one does not work
<?php
$glbs = require 'gen.php';
function decrypt($enctext){
global $glbs;
$data = base64_decode($enctext);
$ivsize = openssl_cipher_iv_length($glbs['myenctype']);
$iv = substr($data, $glbs['myencoption'], $ivsize);
$encrypted = substr($data, $ivsize);
return openssl_decrypt($encrypted, $glbs['myenctype'], $glbs['myenckey'], $glbs['myencoption'], $iv);
}
The error received is that the array $glbs is empty? See below

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\MOJB1\includes\func2.php on line 7

Fatal error
: Uncaught ValueError: openssl_cipher_iv_length(): Argument #1 ($cipher_algo) cannot be empty in C:\xampp\htdocs\MOJB1\includes\func2.php:7 Stack trace: #0 C:\xampp\htdocs\MOJB1\includes\func2.php(7): openssl_cipher_iv_length('') #1 C:\xampp\htdocs\MOJB1\includes\create_acc_view.php(67): decrypt('8PffsVYGSlrtMlF...') #2 C:\xampp\htdocs\MOJB1\create_accountno.php(22): show_authaccts() #3 {main} thrown in C:\xampp\htdocs\MOJB1\includes\func2.php on line 7

Looking at the 2 I can see no reason why Function 1 works but Function 2 does not. I have tried them in different php file, in the same file - it makes no difference. The only way I've got Function 2 to work is to place the key, option and method type values directly into the function (so not use the array). Then it works perfectly decrypting mySQL database values that are passed as its parameter.

Can any one help on this please.

PHP is version 8.2.12.0 running on a localhost server via XAMPP Apache.

The strange thing is that they both worked when under PHP version 8.3.16 which I was using before downloading and using XAMPP which is still using the older version. So may be it is a bug already fixed?
Ensure that the path to gen.php is correct. If func2.php is in a different directory, you may need to adjust the path accordingly.
Open gen.php and ensure it returns the $xarry array correctly. It should look something like this:
Code:
<?php
$xarry = [
    'myenckey' => 'ajD.............................qq:(', // changed to hide the actual value being used
    'myenctype' => 'a.....c', // changed to hide the actual value being used
    'myencoption' => 0,
];
return $xarry;
The line where you extract the IV in the decryption function should be corrected. It should use the correct starting position for the IV:
Code:
$iv = substr($data, 0, $ivsize); // Change $glbs['myencoption'] to 0
 
Thank you for your reply.

I double checked the directory paths and that was not the problem.
I fact in the original set up I have not determined what was causing the problem.

I removed the variable $xarry from gen.php and did a straight return of the array as follows
Code:
<?php
   return [
  'myenckey' => 'aj....................:(',
  'myenctype' => 'a........c',
  'myencoption' =>  0 ];

I also changed how the 2 functions are used by passing $glbs; as a 2nd parameter, having assigned the values from gen.php; just prior to calling the functions. Rather than trying to assign values to $glbs within the func.php file and using the
Code:
global $glbs;
command.

They both now work as expected.
 
Back
Top