key = md5( (string) $enc_key ); $this->key_type = (string) $key_type; $this->deprecated = (bool) version_compare( PHP_VERSION, '7.1', '>=' ); /** @TODO: Should we do something here to inform user about deprecation? */ } /** * * Encrypt a string (Passwords) * * @param string $string value to encrypt * * @return string encrypted string */ public function encrypt( $string ) { if ( ! is_string( $string ) || ! $string ) { return ''; } $encrypted = $this->deprecated ? @mcrypt_encrypt( MCRYPT_RIJNDAEL_256, $this->key, $string, MCRYPT_MODE_CBC, md5( $this->key ) ) : mcrypt_encrypt( MCRYPT_RIJNDAEL_256, $this->key, $string, MCRYPT_MODE_CBC, md5( $this->key ) ); return BackWPup_Encryption::PREFIX . self::PREFIX . $this->key_type . base64_encode( $encrypted ); } /** * * Decrypt a string (Passwords) * * @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 ''; } if ( defined( 'BACKWPUP_MCRYPT_KEY_MODE' ) && BACKWPUP_MCRYPT_KEY_MODE === 1 ) { return $this->decrypt_deprecated( $encrypted ); } $decrypted = $this->deprecated ? @mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $this->key, $encrypted, MCRYPT_MODE_CBC, md5( $this->key ) ) : mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $this->key, $encrypted, MCRYPT_MODE_CBC, md5( $this->key ) ); $skip_deprecated = defined( 'BACKWPUP_MCRYPT_KEY_MODE' ) && BACKWPUP_MCRYPT_KEY_MODE === 2; if ( ! $skip_deprecated && ! @wp_check_invalid_utf8( $decrypted ) ) { $decrypted = $this->decrypt_deprecated( $encrypted ); } return $decrypted; } /** * @param string $encrypted * * @return string */ private function decrypt_deprecated( $encrypted ) { $key = md5( $this->key ); return $this->deprecated ? @mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CBC, md5( $key ) ) : mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_CBC, md5( $key ) ); } }