搜索
首页php教程php手册PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法,mcryptmoduleopen

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>?>

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
2 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中