function fnEncrypt($key,$string){ 
 
    return  base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, md5($key))); 
    } 

fnEncrypt("6abd9b547f8d23b367dff1fc4g7ad457b7f7f85e","AB10|13213123"); 

我收到类似这样的警告信息

Warning: mcrypt_encrypt(): Size of key is too large for this algorithm

如何解决?提前致谢

请您参考如下方法:

您的 key 太长,正如警告消息所示。

自 5.6.0 起,mcrypt_encrypt :

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 '\0' bytes to the next valid size.

AES-256 的 key 大小为 32 字节;您的字符串长度为 40 个字符。

另外,看看 Example 1在那个页面上。这表明

the key should be random binary, use scrypt, bcrypt or PBKDF2 to convert a string into a key

这意味着,不要直接使用字符串作为键(就像您正在做的那样)。 key 派生函数不仅更安全,而且允许您使用任意长度的密码短语,同时生成正确的 key 长度。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!