使用新 php 版本加密解密问题

Encrypt decrypt issue with new php version

我在 PHP 5.3.29 上 运行 的一个网站上有加密功能 该函数在此版本 PHP 上正常工作。函数为:

function encrypt($text) { 
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); 
}

我有另一个网站 运行 PHP 5.6.29。同样的功能在这个版本上没有 return 任何东西。它 return 是空白的。

同样,我有解密功能,但在 PHP 5.6.29

上也不起作用
function decrypt($text) { 
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, SALT, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); 
}

我需要使此功能在 PHP 5.6.29 上运行,因为我的网站是通过 API 连接的。我不知道如何使它起作用。有什么帮助吗?

您传递的 SALT 值可能不正确。来自手册:

Invalid key and iv sizes are no longer accepted. mcrypt_encrypt() will now throw a warning and return FALSE if the inputs are invalid. Previously keys and IVs were padded with '[=10=]' bytes to the next valid size.

这是对 PHP 5.6 所做的更改,与您所看到的一致。

请注意,加密密钥与散列盐不同,散列盐通常可以是任意长度。

我认为您对 SALT 有疑问。如果您将提供 32 个字符的 SALT,那么这些函数也会在 PHP 5.6.29 中提供输出。