我有以下示例代码使用mcrypt与php:
/* Open the cipher */
$td = mcrypt_module_open('tripledes', '', 'ofb', '');
/* Create the IV and determine the keysize length, use MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
/* Create key */
$key = substr(md5('this is a very good key'), 0, $ks);
/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);
/* Encrypt data */
$encrypted = mcrypt_generic($td, 'This is very important data');
/* Terminate encryption handler */
mcrypt_generic_deinit($td);
/* Initialize encryption module for decryption */
mcrypt_generic_init($td, $key, $iv);
/* Decrypt encrypted string */
$decrypted = mdecrypt_generic($td, $encrypted);
/* Terminate decryption handle and close module */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
/* Show string */
echo trim($decrypted) . "\n<br>";
此代码工作正常,但我必须使用&#34; enigma&#34;而不是三元组。
如果我将第一行更改为:
mcrypt_module_open('enigma', '', 'ofb', '');
我收到以下错误:
Warning: mcrypt_module_open(): Could not open encryption module in /Applications/MAMP/htdocs/test/mcrypt.php on line 13
Warning: mcrypt_enc_get_iv_size() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 17
Warning: mcrypt_create_iv(): Cannot create an IV with a size of less than 1 or greater than 2147483647 in /Applications/MAMP/htdocs/test/mcrypt.php on line 17
Warning: mcrypt_enc_get_key_size() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 18
Warning: mcrypt_generic_init() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 24
Warning: mcrypt_generic() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 27
Warning: mcrypt_generic_deinit() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 30
Warning: mcrypt_generic_init() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 33
Warning: mdecrypt_generic() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 36
Warning: mcrypt_generic_deinit() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 39
Warning: mcrypt_module_close() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/test/mcrypt.php on line 40
我不知道为什么会发生这种情况以及如何解决这个问题。如果我使用以下方法检查可用模式:
$modes = mcrypt_list_modes();
foreach ($modes as $mode) {
echo "$mode <br />\n";
}
我得到以下内容:
cbc
cfb
ctr
ecb
ncfb
nofb
ofb
stream
通过检查可用的算法:
$algorithms = mcrypt_list_algorithms("/usr/local/lib/libmcrypt");
foreach ($algorithms as $cipher) {
echo "$cipher<br />\n";
}
我收到了这个输出:
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
我希望有人知道问题是什么以及如何解决这个问题。谢谢!