如何在WAMP中使用MCRYPT_ARCFOUR进行加密?

时间:2011-11-07 18:30:55

标签: php cryptography wamp mcrypt

我正在尝试在我的PHP代码中使用ARCFOUR算法:

$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);

问题是第一行返回警告:

mcrypt_module_open(): Could not open encryption module

我的设置:

  • 来自php_info()输出:

    命令configure:...“ - with-mcrypt = static”...

    如果我是正确的,这意味着我不需要用于mcrypt扩展的DLL。

    支持的密码: 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版本5.3.8

  • Wamp 2.2a(32位)

谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

自PHP 5.3.6和5.4.0 RC6 起,arcfourwakeenigma mcrypt密码需要使用{ {1}}模式。它们不会初始化任何其他模式,任何其他密码都不能使用stream模式。

可能也适用于早期的PHP版本。

演示代码,stream存在以使“无法打开模块”警告静音:

@

演示输出:

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";

看起来你(和我)看到PHP bug 49311,在2009年没有反馈后关闭了。RC4WAKE和Enigma被打破了。用于演示问题的代码:

        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

我的系统上的输出,来自PHP交互式提示:

foreach(mcrypt_list_algorithms() as $algo) {
    echo $algo; 
    $td = mcrypt_module_open($algo, '', MCRYPT_MODE_CBC, '');
    echo "\n";
}

在(除非)他们修复此错误或找到解决方法之前,您需要选择其他加密算法。

答案 1 :(得分:1)

这三个是流密码。所有其他密码都是块密码。他们不能使用相同的模式。

所以只需使用MCRYPT_MODE_STREAM你正在使用MCRYPT_MODE_CBC,你应该是金色的。

如果没有,请使用以下内容:

/** 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);
}

适合我。