问题
I'm trying to use ARCFOUR algorithm in my PHP code:
$td = mcrypt_module_open(MCRYPT_ARCFOUR, '', MCRYPT_MODE_CBC, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$output = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
and the problem is that the first line returns warning:
mcrypt_module_open(): Could not open encryption module
My settings:
From php_info() output:
command configure: ... "--with-mcrypt=static" ...
If I'm correct it means I do not need a DLL for mcrypt extension.
Supported ciphers: cast-128 gost rijndael-128 twofish arcfour cast-256 loki97 rijndael-192 saferplus wake blowfish-compat des rijndael-256 serpent xtea blowfish enigma rc2 tripledes
PHP version 5.3.8
Wamp 2.2a (32 bits)
Thank you for help!
回答1:
As of PHP 5.3.6 and 5.4.0 RC6, the arcfour
, wake
and enigma
mcrypt ciphers require the use of stream
mode. They will not initialize any of the other modes, nor can any of the other ciphers use stream
mode.
This may be true of earlier PHP versions as well.
Demo code, with the @
present to silence the "can't open module" warning:
foreach(mcrypt_list_algorithms() as $alg) {
printf("\n%20s:", $alg);
foreach(mcrypt_list_modes() as $mode) {
$mc = @mcrypt_module_open($alg, null, $mode, null);
if(is_resource($mc)) echo "\t$mode";
else echo "\t!!FAIL!!:$mode";
}
}
echo "\n";
Demo output:
cast-128: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
gost: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
rijndael-128: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
twofish: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
arcfour: !!FAIL!!:cbc !!FAIL!!:cfb !!FAIL!!:ctr !!FAIL!!:ecb !!FAIL!!:ncfb !!FAIL!!:nofb !!FAIL!!:ofb stream
cast-256: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
loki97: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
rijndael-192: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
saferplus: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
wake: !!FAIL!!:cbc !!FAIL!!:cfb !!FAIL!!:ctr !!FAIL!!:ecb !!FAIL!!:ncfb !!FAIL!!:nofb !!FAIL!!:ofb stream
blowfish-compat: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
des: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
rijndael-256: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
serpent: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
xtea: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
blowfish: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
enigma: !!FAIL!!:cbc !!FAIL!!:cfb !!FAIL!!:ctr !!FAIL!!:ecb !!FAIL!!:ncfb !!FAIL!!:nofb !!FAIL!!:ofb stream
rc2: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
tripledes: cbc cfb ctr ecb ncfb nofb ofb !!FAIL!!:stream
It looks like you (and I) are seeing PHP bug 49311, which was closed after no feedback in 2009. RC4, WAKE and Enigma are broken. Code to demonstrate the problem:
foreach(mcrypt_list_algorithms() as $algo) {
echo $algo;
$td = mcrypt_module_open($algo, '', MCRYPT_MODE_CBC, '');
echo "\n";
}
Output on my system, from the PHP interactive prompt:
cast-128
gost
rijndael-128
twofish
arcfourPHP Warning: mcrypt_module_open(): Could not open encryption module in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
PHP 2. mcrypt_module_open() php shell code:1
cast-256
loki97
rijndael-192
saferplus
wakePHP Warning: mcrypt_module_open(): Could not open encryption module in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
PHP 2. mcrypt_module_open() php shell code:1
blowfish-compat
des
rijndael-256
serpent
xtea
blowfish
enigmaPHP Warning: mcrypt_module_open(): Could not open encryption module in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
PHP 2. mcrypt_module_open() php shell code:1
rc2
tripledes
Until (unless) they fix this bug or a workaround is found, you'll want to choose a different encryption algorithm.
回答2:
These three are stream cyphers. All other cyphers are block cyphers. They can't use the same modes as each other.
So just use MCRYPT_MODE_STREAM where you are using MCRYPT_MODE_CBC and you should be golden.
If not, use something like this:
/** Encrypts text with ARC4 algorithm. */
public static function encryptArc4($key, $data) {
$iv = ""; // Empty init vector.
return mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $data, MCRYPT_MODE_STREAM, $iv);
}
/** Decrypts text with ARC4 algorithm. */
public static function decryptArc4($key, $data) {
$iv = ""; // Empty init vector.
return mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $data, MCRYPT_MODE_STREAM, $iv);
}
Works for me.
来源:https://stackoverflow.com/questions/8040901/how-to-encrypt-with-mcrypt-arcfour-in-wamp