PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法
我也是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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

Dreamweaver Mac版
시각적 웹 개발 도구

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

맨티스BT
Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기
