OS : Linux
PHP Version : 7.4.33
Software : Apache/2.4.6 (CentOS) PHP/7.4.33
Information System : Linux apprendre2 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64key = md5((string) $enc_key); $this->key_type = (string) $key_type; } /** * Supported. * * @return bool */ public static function supported() { return function_exists('openssl_get_cipher_methods') && self::cipher_method(); } /** * Encrypt a string using Open SSL lib with AES-256-CTR cypher. * * @param string $string value to encrypt * * @return string encrypted string */ public function encrypt($string) { if (!is_string($string) || !$string) { return ''; } $nonce = openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::cipher_method())); $openssl_raw_data = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : 1; $encrypted = openssl_encrypt( $string, self::cipher_method(), $this->key, $openssl_raw_data, $nonce ); return BackWPup_Encryption::PREFIX . self::PREFIX . $this->key_type . base64_encode($nonce . $encrypted); } /** * Decrypt a string using Open SSL lib with AES-256-CTR cypher. * * @param string $string value to decrypt * * @return string decrypted string */ public function decrypt($string) { if ( !is_string($string) || !$string || strpos($string, BackWPup_Encryption::PREFIX . self::PREFIX . $this->key_type) !== 0 ) { return ''; } $no_prefix = substr( $string, strlen(BackWPup_Encryption::PREFIX . self::PREFIX . $this->key_type) ); $encrypted = base64_decode($no_prefix, true); if ($encrypted === false) { return ''; } $nonce_size = openssl_cipher_iv_length(self::cipher_method()); $nonce = substr($encrypted, 0, $nonce_size); $to_decrypt = substr($encrypted, $nonce_size); $openssl_raw_data = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true; return openssl_decrypt( $to_decrypt, self::cipher_method(), $this->key, $openssl_raw_data, $nonce ); } /** * Cipher Method. * * @return string */ private static function cipher_method() { if (is_string(self::$cipher_method)) { return self::$cipher_method; } $all_methods = openssl_get_cipher_methods(); if (!$all_methods) { self::$cipher_method = ''; return ''; } $preferred = ['AES-256-CTR', 'AES-128-CTR', 'AES-192-CTR']; foreach ($preferred as $method) { if (in_array($method, $all_methods, true)) { self::$cipher_method = $method; return $method; } } self::$cipher_method = reset($all_methods); return self::$cipher_method; } }