>php教程 >php手册 >PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法 - StarkSoft

PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法 - StarkSoft

WBOY
WBOY원래의
2016-05-20 11:51:511167검색

我也是PHP新手,通过w3cschool了解了一下php基本原理之后就开写了。但仍是菜鸟。

先不管3DES加密的方法对不对,方法都是网上的,在运行的时候报了个错,把小弟整死了。找来找去终于自己摸出了方法。

<span style="color: #000000;">php

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
* 
* PHP版3DES加解密类
*
* 可与java的3DES(DESede)加密方式兼容
*
* @Author: Luo Hui (farmer.luo at gmail.com)
*
* @version: V0.1 2008.12.04
*
</span><span style="color: #008000;">*/</span>


<span style="color: #0000ff;">class</span><span style="color: #000000;"> Crypt3Des
{    
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$key</span>    = "01234567890123456789012345678912"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$iv</span>    = "23456789"; <span style="color: #008000;">//</span><span style="color: #008000;">like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
    
    //加密</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> encrypt(<span style="color: #800080;">$input</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$input</span> = <span style="color: #800080;">$this</span>->padding( <span style="color: #800080;">$input</span><span style="color: #000000;"> );
        </span><span style="color: #800080;">$key</span> = <span style="color: #008080;">base64_decode</span>(<span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$td</span> = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''<span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">使用MCRYPT_3DES算法,cbc模式</span>
        mcrypt_generic_init(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$key</span>, <span style="color: #800080;">$this</span>-><span style="color: #000000;">iv);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">初始处理</span>
        <span style="color: #800080;">$data</span> = mcrypt_generic(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$input</span><span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">加密</span>
        mcrypt_generic_deinit(<span style="color: #800080;">$td</span><span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">结束</span>
        mcrypt_module_close(<span style="color: #800080;">$td</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$data</span> = <span style="color: #800080;">$this</span>->removeBR(<span style="color: #008080;">base64_encode</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">));
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">解密</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> decrypt(<span style="color: #800080;">$encrypted</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$encrypted</span> = <span style="color: #008080;">base64_decode</span>(<span style="color: #800080;">$encrypted</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$key</span> = <span style="color: #008080;">base64_decode</span>(<span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$td</span> = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,''<span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">使用MCRYPT_3DES算法,cbc模式</span>
        mcrypt_generic_init(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$key</span>, <span style="color: #800080;">$this</span>-><span style="color: #000000;">iv);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">初始处理</span>
        <span style="color: #800080;">$decrypted</span> = mdecrypt_generic(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$encrypted</span><span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">解密</span>
        mcrypt_generic_deinit(<span style="color: #800080;">$td</span><span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">结束</span>
        mcrypt_module_close(<span style="color: #800080;">$td</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$decrypted</span> = <span style="color: #800080;">$this</span>->removePadding(<span style="color: #800080;">$decrypted</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$decrypted</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">填充密码,填充至8的倍数</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> padding( <span style="color: #800080;">$str</span><span style="color: #000000;"> )
    {
        </span><span style="color: #800080;">$len</span> = 8 - <span style="color: #008080;">strlen</span>( <span style="color: #800080;">$str</span> ) % 8<span style="color: #000000;">;
        </span><span style="color: #0000ff;">for</span> ( <span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $len; <span style="color: #800080;">$i</span>++<span style="color: #000000;"> )
        {
            </span><span style="color: #800080;">$str</span> .= <span style="color: #008080;">chr</span>( 0<span style="color: #000000;"> );
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$str</span><span style="color: #000000;"> ;
    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">删除填充符</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> removePadding( <span style="color: #800080;">$str</span><span style="color: #000000;"> )
    {
        </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">strlen</span>( <span style="color: #800080;">$str</span><span style="color: #000000;"> );
        </span><span style="color: #800080;">$newstr</span> = ""<span style="color: #000000;">;
        </span><span style="color: #800080;">$str</span> = <span style="color: #008080;">str_split</span>(<span style="color: #800080;">$str</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $len; <span style="color: #800080;">$i</span>++<span style="color: #000000;"> )
        {
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span>] != <span style="color: #008080;">chr</span>( 0<span style="color: #000000;"> ))
            {
                </span><span style="color: #800080;">$newstr</span> .= <span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
            }
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$newstr</span><span style="color: #000000;">;
    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">删除回车和换行</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> removeBR( <span style="color: #800080;">$str</span><span style="color: #000000;"> ) 
    {
        </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">strlen</span>( <span style="color: #800080;">$str</span><span style="color: #000000;"> );
        </span><span style="color: #800080;">$newstr</span> = ""<span style="color: #000000;">;
        </span><span style="color: #800080;">$str</span> = <span style="color: #008080;">str_split</span>(<span style="color: #800080;">$str</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $len; <span style="color: #800080;">$i</span>++<span style="color: #000000;"> )
        {
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span>] != '\n' and <span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span>] != '\r'<span style="color: #000000;">)
            {
                </span><span style="color: #800080;">$newstr</span> .= <span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
            }
        }
    
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$newstr</span><span style="color: #000000;">;
    }

}

</span><span style="color: #008000;">//</span><span style="color: #008000;">test</span>
<span style="color: #800080;">$input</span> = "1qaz2ws"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "plainText:" . <span style="color: #800080;">$input</span>."<br>"<span style="color: #000000;">;
</span><span style="color: #800080;">$crypt</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Crypt3Des();
</span><span style="color: #0000ff;">echo</span> "Encode:".<span style="color: #800080;">$crypt</span>->encrypt(<span style="color: #800080;">$input</span>)."<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "Decode:".<span style="color: #800080;">$crypt</span>->decrypt(<span style="color: #800080;">$crypt</span>->encrypt(<span style="color: #800080;">$input</span><span style="color: #000000;">));
</span>?>

代码可以不看,就看里面的一句:$td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');报错的就是他。

我搜寻了一大堆解决方法,正确的方法应该是(仅用于windows系统哦):

当运行php的服务器端缺少libmcrypt.dll时使用函数mcrypt_module_open进行解密会出现此错误。 
在服务器上做如下设置可解决。

到网上下载一个php的mcrypt模块安装包,只需要libmcrypt.dll文件即可(一般官网上下载的,php目录下已经有的)

1.将libmcrypt.dll复制到system32目录或php安装目录下的extensions目录下 
2.将libmcrypt.dll复制到apache安装目录的bin目录下 
3.到windows目录下找到php.ini文件,打开它
4.找到; Directory in which the loadable extensions (modules) reside. 
extension_dir = "./"  如:extension_dir = "D:\php5\ext"
这两行,要使extension_dir指向的目录下能找到libmcrypt.dll,或系统path下有libmcrypt.dll 
5.找到;Windows Extensions 项下面的;extension=php_mcrypt.dll这一行和;extension=php_iconv.dll(我的没有,省略了)这两行,去掉前面的分号

ps:刚开始看网上的解决方法,有的说修改php安装目录下的php.ini,但是修改后是没用的。一定要修改windows目录下的php.ini!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.