Skip to content

Início / Optitravel / Security

Data Encrypt, Decrypt and Mask

Resume

Data encrypt and decrypt is a feature that adds an extra layer of security to the systems by encrypting data. Additionally there are mask feature that allows protection of sensitive data.

Encrypt / Decrypt Usage

The following code sample shows how to encrypt and decrypt data.

// Instantiate object (includes vault keys read into object)
$crypt = new \App\security\EncryptDecryptData();
$crypt_algorithm = "AES-256-GCM"; // The encryptation algorithm to use
$crypt_key_index = $crypt->getActiveIndexFromAlgorithm($crypt_algorithm); // Get the active key for algorithm

// Encrypt data
$encrypted_data = $crypt->encryptData($crypt_algorithm, $crypt_key_index, $data, $base64_output = true);

// Decrypt data
$data = $crypt->decryptData($crypt_algorithm, $crypt_key_index, $encrypted_data, $base64_source = true);

// Instead you can use a wrapper to get data that can be in plain-text or encrypted
$data = Data::getPlainDataFromPossibleEncrypted($crypt_algorithm, $crypt_key_index, $plain_or_encrypted_data);

It is recomended to use base64 encoding at all time (encrypting and decrypting) to avoid encoding problems.

Available Algorithms

At this point the available algorithms are: - AES-256-GCM

You can get the algorithm list from the object as the next code sample.

$algorithms = $crypt->getAvailableAlgorithms();

Mask Usage

The following code sample shows how to mask a string.

$masked_data = Data::maskString($string = "text to mask", $percentage = 60, $minMask = 4, $maskChar = '*');
// echo maskString("joao.moreira@gmail.com");
// joa***************.com

↑ (Última atualização: 05/11/2024)