phpseclib/src/a25c82c8-14c2-40df-adae-f83.../code.power

103 lines
2.1 KiB
Plaintext

/**
* The Aes class
*
* @var BASEAES
* @since 3.2.0
*/
protected BASEAES $aes;
/**
* The Random class
*
* @var Random
* @since 3.2.0
*/
protected Random $random;
/**
* The block size
*
* @var int
* @since 3.2.0
*/
protected int $size = 256;
/**
* Constructor
*
* @param BASEAES $aes The Aes class
* @param Random $random The Random class
*
* @since 3.2.0
*/
public function __construct(BASEAES $aes, Random $random)
{
$this->aes = $aes;
$this->random = $random;
// we set the length once
$this->aes->setKeyLength($this->size);
// enable padding
$this->aes->enablePadding();
}
/**
* Encrypt a string as needed
*
* @param string $string The string to encrypt
* @param string $key The encryption key
*
* @return string
* @since 3.2.0
**/
public function encrypt(string $string, string $key): string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// get the IV value
$iv = $this->random::string($iv_length);
// Load the IV
$this->aes->setIV($iv);
// set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
// encrypt the string, and base 64 encode the result
return base64_encode($iv . $this->aes->encrypt($string));
}
/**
* Decrypt a string as needed
*
* @param string $string The string to decrypt
* @param string $key The decryption key
*
* @return string
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// remove base 64 encoding
$string = base64_decode($string);
// get the IV
$iv = substr($string, 0, $iv_length);
// remove the IV
$string = substr($string, $iv_length);
// set the IV
$this->aes->setIV($iv);
// set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
return $this->aes->decrypt($string);
}