public static function AesDecrypt($encrypted, $key= null){if($encrypted=='')return'';$cipher="aes-128-cbc";if(!in_array($cipher, openssl_get_cipher_methods())){
throw new Exception("不支持的加密方法: ".$cipher);}$ciphertext_dec= base64_decode($encrypted);
// OpenSSL expects a key of 16, 24 or 32 bytes length
// depending on the cipher and mode
#$key = self::mbSubstr($key, 0, 32); // AES-256 uses 32 bytes#这里是关键 $key= base64_decode($key);
// OpenSSL expects an IV of 16 bytes
$iv= str_repeat("\0", 16);
// OpenSSL uses the same key for encryption and decryption
$decrypted= openssl_decrypt($ciphertext_dec,
'AES-128-CBC', // Use AES-128-CBC as an example
$key,
OPENSSL_RAW_DATA, // Flag to return the raw data
$iv);
// No need to rtrim() as openssl_decrypt() doesn't pad with null bytes
// $a = rtrim($decrypted, "\0"); // This line is not needed
// Ensure the decrypted text is not empty and return
return $decrypted ?? '';}