PHP code example of helppc / encryption-bundle

1. Go to this page and download the library: Download helppc/encryption-bundle 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/ */

    

helppc / encryption-bundle example snippets


return [
    // ...
    HelpPC\EncryptionBundle\EncryptionBundle::class => ['all' => true],
];

// config/packages/encryption.php
use HelpPC\EncryptionBundle\EncryptionType;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\env;

return static function (ContainerConfigurator $container): void {
    $container->extension('encryption', [
        'default_group' => 'default',
        'groups' => [
            'default' => [
                'type' => EncryptionType::Symmetric,
                'key_prefix' => 'adek',
                'active_key' => 'v2',
                'keys' => [
                    'v1' => env('ENCRYPTION_KEY_V1'),
                    'v2' => env('ENCRYPTION_KEY_V2'),
                ],
            ],
            'partner_inbox' => [
                'type' => EncryptionType::AnonymousAsymmetric,
                'key_prefix' => 'partner',
                'active_key' => 'v1',
                'keys' => [
                    'v1' => ['public_key' => env('PARTNER_PUBLIC_KEY')],
                ],
            ],
        ],
    ]);
};

use HelpPC\EncryptionBundle\Encryption\Decryptor;
use HelpPC\EncryptionBundle\Encryption\Encryptor;

public function __construct(
    private Encryptor $encryptor,
    private Decryptor $decryptor,
) {
}

use Symfony\Component\DependencyInjection\Attribute\Target;

public function __construct(
    #[Target('vault')] private Decryptor $vault,
    #[Target('partner_inbox')] private Encryptor $partnerInbox,
) {
}

use HelpPC\EncryptionBundle\Encryption\AdditionalDataDecryptor;
use HelpPC\EncryptionBundle\Encryption\AdditionalDataEncryptor;

public function __construct(
    private AdditionalDataEncryptor $aadEncryptor,
    private AdditionalDataDecryptor $aadDecryptor,
) {
}

$cipherText = $this->aadEncryptor->encryptWithAdditionalData($addressData, $tenantId);
$plainText  = $this->aadDecryptor->decryptWithAdditionalData($cipherText, $tenantId);

if ($this->encryptor->needsReEncryption($cipherText)) {
    $cipherText = $this->encryptor->encrypt($this->decryptor->decrypt($cipherText));
}