1. Go to this page and download the library: Download phpsoftbox/encryptor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpsoftbox / encryptor example snippets
declare(strict_types=1);
use PhpSoftBox\Encryptor\Encryptor;
$encryptor = new Encryptor(defaultKey: $_ENV['APP_KEY']);
$ciphertext = $encryptor->encrypt('payload', $_ENV['APP_KEY']);
$plain = $encryptor->decrypt($ciphertext, $_ENV['APP_KEY']);
use PhpSoftBox\Encryptor\Encryptor;
use PhpSoftBox\Encryptor\Key\ArrayKeyProvider;
$encryptor = new Encryptor(
keyProvider: new ArrayKeyProvider($_ENV['APP_KEY']),
);
$ciphertext = $encryptor->encryptWithCurrentKey('payload');
$plain = $encryptor->decryptWithAnyKey($ciphertext);
use PhpSoftBox\Encryptor\Encryptor;
$encryptor = new Encryptor();
$ciphertext = $encryptor->encryptWithDriver('openssl', 'payload', $_ENV['APP_KEY']);
$plain = $encryptor->decryptWithDriver('openssl', $ciphertext, $_ENV['APP_KEY']);
use PhpSoftBox\Encryptor\Contracts\DriverInterface;
use PhpSoftBox\Encryptor\Encryptor;
final class MyDriver implements DriverInterface
{
public function name(): string
{
return 'my-driver';
}
public function encrypt(string $plaintext, string $key, string $associatedData = ''): string
{
// ...
}
public function decrypt(string $ciphertext, string $key, string $associatedData = ''): string
{
// ...
}
}
$encryptor = new Encryptor();
$encryptor->registerDriver(new MyDriver());
$ciphertext = $encryptor->encryptWithDriver('my-driver', 'payload', $_ENV['APP_KEY']);
$plain = $encryptor->decryptWithDriver('my-driver', $ciphertext, $_ENV['APP_KEY']);
use PhpSoftBox\Encryptor\EncryptedValue;
use PhpSoftBox\Encryptor\Encryptor;
$encryptor = new Encryptor(defaultKey: $_ENV['APP_KEY']);
$ciphertext = $encryptor->encrypt('payload', $_ENV['APP_KEY']);
$value = new EncryptedValue($ciphertext);
$plain = $encryptor->resolve($value); // payload
use PhpSoftBox\Encryptor\EncryptedValue;
use PhpSoftBox\Encryptor\Encryptor;
$encryptor = new Encryptor(defaultKey: $keyV1);
$ciphertext = $encryptor->encrypt('payload', $keyV2);
$value = new EncryptedValue($ciphertext, key: $keyV2, driver: 'openssl');
$plain = $encryptor->resolve($value);
use PhpSoftBox\Encryptor\Encryptor;
use PhpSoftBox\Encryptor\Key\ArrayKeyProvider;
$provider = new ArrayKeyProvider(
currentKey: $_ENV['APP_KEY'],
previousKeys: [$_ENV['APP_PREVIOUS_KEY']],
);
$encryptor = new Encryptor(keyProvider: $provider);
$ciphertext = $encryptor->encryptWithCurrentKey('payload'); // шифруется текущим ключом
$plain = $encryptor->decryptWithAnyKey($ciphertext); // пробует текущий, затем предыдущие
use DI\ContainerBuilder;
use PhpSoftBox\Encryptor\Contracts\EncryptedValueResolverInterface;
use PhpSoftBox\Encryptor\Contracts\KeyProviderInterface;
use PhpSoftBox\Encryptor\Encryptor;
use PhpSoftBox\Encryptor\Key\ArrayKeyProvider;
$builder = new ContainerBuilder();
$builder->addDefinitions([
KeyProviderInterface::class => static function (): KeyProviderInterface {
$currentKey = (string) ($_ENV['APP_KEY'] ?? '');
$previousRaw = (string) ($_ENV['APP_PREVIOUS_KEYS'] ?? '');
$previousKeys = array_filter(array_map('trim', explode(',', $previousRaw)));
return new ArrayKeyProvider($currentKey, $previousKeys);
},
Encryptor::class => static function (KeyProviderInterface $keys): Encryptor {
return new Encryptor(keyProvider: $keys);
},
EncryptedValueResolverInterface::class => static function (Encryptor $encryptor): EncryptedValueResolverInterface {
return $encryptor;
},
]);
$container = $builder->build();
$encryptor = $container->get(Encryptor::class);
use PhpSoftBox\Config\Config;
use PhpSoftBox\Encryptor\EncryptedValue;
use PhpSoftBox\Encryptor\Encryptor;
$encryptor = new Encryptor(defaultKey: $_ENV['APP_KEY']);
$hash = $encryptor->encrypt('payload', $_ENV['APP_KEY']);
$config = new Config(
[
['secret' => new EncryptedValue($hash)],
],
encryptedValueResolver: $encryptor,
);
$plain = $config->get('secret'); // payload
use PhpSoftBox\Config\ConfigFactory;
use PhpSoftBox\Encryptor\Encryptor;
$factory = new ConfigFactory(
baseDir: __DIR__ . '/..',
encryptedValueResolver: new Encryptor(defaultKey: $_ENV['APP_KEY'])
);
$config = $factory->create();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.