PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法,mcryptmoduleopen_PHP教程

WBOY
Lepaskan: 2016-07-12 08:54:27
asal
1431 orang telah melayarinya

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

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

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

<?<span>php

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


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

}

</span><span>//</span><span>test</span>
<span>$input</span> = "1qaz2ws"<span>;
</span><span>echo</span> "plainText:" . <span>$input</span>."<br/>"<span>;
</span><span>$crypt</span> = <span>new</span><span> Crypt3Des();
</span><span>echo</span> "Encode:".<span>$crypt</span>->encrypt(<span>$input</span>)."<br/>"<span>;
</span><span>echo</span> "Decode:".<span>$crypt</span>->decrypt(<span>$crypt</span>->encrypt(<span>$input</span><span>));
</span>?>
Salin selepas log masuk

代码可以不看,就看里面的一句:$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!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1120376.htmlTechArticlePHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法,mcryptmoduleopen 我也是PHP新手,通过w3cschool了解了一下php基本原理之后就开...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan