Skip to content

Início / Optitravel / Security

Data Encrypt and Decrypt

Resume

Data encrypt and decrypt is a feature that adds an extra layer of security to the systems by encrypting data.

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();

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