- md5
- 2791bbc5cec1f42d3111cce79d118588
- sha1
- 5dc07f468b7e501a2bda7ccc37c79c55131a884c
- sha256
- 2d203613b52e47b2421d2df8ac8b76271541933cf5c8e47feecba6f96101c952
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
<?php
/**
* @package Joomla.Administrator
* @subpackage com_users
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Users\Administrator\Service;
use Joomla\CMS\Encrypt\Aes;
use Joomla\CMS\Factory;
/**
* Data encryption service.
*
* @since 4.2.0
*/
class Encrypt
{
/**
* The encryption engine used by this service
*
* @var Aes
* @since 4.2.0
*/
private $aes;
/**
* EncryptService constructor.
*
* @since 4.2.0
*/
public function __construct()
{
$this->initialize();
}
/**
* Encrypt the plaintext $data and return the ciphertext prefixed by ###AES128###
*
* @param string $data The plaintext data
*
* @return string The ciphertext, prefixed by ###AES128###
*
* @since 4.2.0
*/
public function encrypt(string $data): string
{
if (!is_object($this->aes)) {
return $data;
}
$this->aes->setPassword($this->getPassword(), false);
$encrypted = $this->aes->encryptString($data, true);
return '###AES128###' . $encrypted;
}
/**
* Decrypt the ciphertext, prefixed by ###AES128###, and return the plaintext.
*
* @param string $data The ciphertext, prefixed by ###AES128###
* @param bool $legacy Use legacy key expansion? Use it to decrypt data encrypted with FOF 3.
*
* @return string The plaintext data
*
* @since 4.2.0
*/
public function decrypt(string $data, bool $legacy = false): string
{
if (substr($data, 0, 12) != '###AES128###') {
return $data;
}
$data = substr($data, 12);
if (!is_object($this->aes)) {
return $data;
}
$this->aes->setPassword($this->getPassword(), $legacy);
$decrypted = $this->aes->decryptString($data, true, $legacy);
// Decrypted data is null byte padded. We have to remove the padding before proceeding.
return rtrim($decrypted, "\0");
}
/**
* Initialize the AES cryptography object
*
* @return void
* @since 4.2.0
*/
private function initialize(): void
{
if (is_object($this->aes)) {
return;
}
$password = $this->getPassword();
if (empty($password)) {
return;
}
$this->aes = new Aes('cbc');
$this->aes->setPassword($password);
}
/**
* Returns the password used to encrypt information in the component
*
* @return string
*
* @since 4.2.0
*/
private function getPassword(): string
{
try {
return Factory::getApplication()->get('secret', '');
} catch (\Exception $e) {
return '';
}
}
}
The same content is in 5 releases, Joomla 4.2.0-beta3 to 4.2.1-rc2. See them all, or every version of this file.