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?
File Gen.php stores the key, option and type to be used in the encryption/decryption.
<?php
$xarry = [
'myenckey' => 'ajD.............................qq

'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: