1. Home
  2. Docs
  3. API Docs
  4. Encryption / Decryption Methods

Encryption / Decryption Methods

Encryption / Decryption Method:

function encryptArr($dbArr = [], $privateKey = NULL)
	{
		$returnArr = [];
		if (!empty($dbArr) && $privateKey) {
			foreach ($dbArr as $key => $value) {
				$returnArr[$key] = $this->EncryptThis($value, $privateKey);
			}
		}
		return $returnArr;
	}

	function EncryptThis($ClearTextData, $ENCRYPTION_KEY)
	{
		$ENCRYPTION_ALGORITHM = 'AES-256-CBC';
		$EncryptionKey = base64_decode($ENCRYPTION_KEY);
		$InitializationVector  = openssl_random_pseudo_bytes(openssl_cipher_iv_length($ENCRYPTION_ALGORITHM));
		$EncryptedText = openssl_encrypt($ClearTextData, $ENCRYPTION_ALGORITHM, $EncryptionKey, 0, $InitializationVector);
		return base64_encode($EncryptedText . '::' . $InitializationVector);
	}

	function decryptArr($dbArr = [], $privateKey = NULL)
	{
		$returnArr = [];
		if (!empty($dbArr) && $privateKey) {
			foreach ($dbArr as $key => $value) {
				$returnArr[$key] = $this->DecryptThis($value, $privateKey);
			}
		}
		return $returnArr;
	}

	

	function DecryptThis($CipherData, $ENCRYPTION_KEY)
	{
		$ENCRYPTION_ALGORITHM = 'AES-256-CBC';
		$EncryptionKey = base64_decode($ENCRYPTION_KEY);
		list($Encrypted_Data, $InitializationVector) = array_pad(explode('::', base64_decode($CipherData), 2), 2, null);
		return openssl_decrypt($Encrypted_Data, $ENCRYPTION_ALGORITHM, $EncryptionKey, 0, $InitializationVector);
	} 
Was this article helpful to you? Yes No

How can we help?