HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/inventory.breadsecret.com/class/Utility/Security.php
<?php
namespace Utility;

class Security {

    private $symmetric_key;
    private $algorithm;

    public static function buildFromConfig() {
        $cfgObj = cfg('system');

	$ret = new Security();
	if (isset($cfgObj['symmetric_key']) && !empty($cfgObj['symmetric_key'])) {
            $ret->setSymmetricKey($cfgObj['symmetric_key']);
        }
	if (isset($cfgObj['algorithm']) && !empty($cfgObj['algorithm'])) {
            $ret->setAlgorithm($cfgObj['algorithm']);
        }

	return $ret;
    }

    public function __construct($algorithm = "aes-256-cbc", $symmetric_key = "12345678") {
	$this->symmetric_key = $symmetric_key;
	$this->algorithm = $algorithm;
    }

    public function setAlgorithm($algorithm) { $this->algorithm = $algorithm; }
    public function setSymmetricKey($symmetric_key) { $this->symmetric_key = $symmetric_key; }

    public function encrypt($payload)
    {
        $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->algorithm));
        $encrypted = openssl_encrypt($payload, $this->algorithm, $this->symmetric_key, 0, $iv);
        return base64_encode($encrypted . '::' . $iv);
    }
    
    public function decrypt($payload)
    {
        list($encrypted_data, $iv) = explode('::', base64_decode($payload), 2);
        return openssl_decrypt($encrypted_data, $this->algorithm, $this->symmetric_key, 0, $iv);
    }

}