• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/security/cipher.php
  • Класс: BitrixMainSecurityCipher
  • Вызов: Cipher::encrypt
public function encrypt($data, $key)
{
	// Initialisation vector: it MUST be different every time
	$iv = openssl_random_pseudo_bytes($this->ivLength, $strong);
	if(!$strong)
	{
		throw new SecurityException("Not a strong initialisation vector.");
	}

	// Hash the key: we shouldn't use the password itself, it can be weak
	$keyHash = openssl_digest($iv.$key, $this->hashAlgorithm, true);

	if($this->calculateHash)
	{
		//store the hash to check on reading
		$dataHash = openssl_digest($data, $this->hashAlgorithm, true);
		$data = $dataHash.$data;
	}

	// Encrypt the data
	$encrypted = openssl_encrypt($data, $this->cipherAlgorithm, $keyHash, OPENSSL_RAW_DATA, $iv);
	if($encrypted === false)
	{
		throw new SecurityException("Encryption failed: ".openssl_error_string());
	}

	// Store IV with encrypted data to use it for decryption
	$res = $iv.$encrypted;

	return $res;
}