多年来,无论是在win/IIS还是在Linux上,我一直在我的php应用程序中大量使用mcrypt。尽管我在Linux服务器上运行PHP 5.4.28,但我刚刚在Windows 8.1 IIS框中升级到PHP 5.6.11。并且mcrypt不再起作用。它不会抛出我可以看到的任何错误;就是行不通。这是我的加密功能:
function Encrypt($text){
global $salt;
if($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))));
else
return "";
}
这在我的linux服务器上工作正常,但是在我的本地Windows框中返回空白。据我了解,mcrypt内置于Windows的php 5.6中,因此不应摆弄扩展名或ini文件。
我想念什么?
最佳答案
让我们一步一步地看一下您的代码。 (主要是外观/空白更改。)
function Encrypt($text)
{
global $salt; // Why not make this a second parameter?
if($text != "") { // An unusual check, for sure
return trim( // base64_encode doesn't leave whitespace
base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256, // This is a non-standard variant of the
// Rijndael cipher. You want to use the
// MCRYPT_RIJNDAEL_128 constant if you
// wanted to use AES here.
$salt, // This is a key, not a salt!
$text,
MCRYPT_MODE_ECB, // ECB mode is the worst mode to use for
// cryptography. Among other reasons, it
// doesn't even use the IV. Search for
// ECB penguins for an idea of why ECB
// mode is such a bad idea.
mcrypt_create_iv(
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),
MCRYPT_RAND // You're using ECB mode so this is a waste
// anyway, but you should use
// MCRYPT_DEV_URANDOM instead of MCRYPT_RAND
)
)
)
);
}
return "";
}
我强烈建议您不要将此功能用于任何用途。这是不安全的。 Don't use ECB mode。
此外,unauthenticated encryption is dangerous和libmcrypt is abandonware。