How can the character encoding and character limit be managed to ensure consistent encryption results between JavaScript and PHP?

To ensure consistent encryption results between JavaScript and PHP, it is important to use the same character encoding and character limit in both languages. This can be achieved by specifying the character encoding (such as UTF-8) and ensuring that the character limit for encryption is consistent in both JavaScript and PHP.

// Set character encoding and character limit for encryption
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');

// Encrypt data using AES encryption in PHP
function encryptData($data, $key) {
    $cipher = "aes-256-cbc";
    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $encrypted = openssl_encrypt($data, $cipher, $key, 0, $iv);
    return base64_encode($iv . $encrypted);
}