Home > php教程 > php手册 > PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 如何解决,mcryptmoduleopen

PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 如何解决,mcryptmoduleopen

WBOY
Release: 2016-06-13 08:41:29
Original
1161 people have browsed it

PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 如何解决,mcryptmoduleopen

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

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

<&#63;php
/**
* 
* PHP版3DES加解密类
*
* 可与java的3DES(DESede)加密方式兼容
*
* @Author: Luo Hui (farmer.luo at gmail.com)
*
* @version: V0.1 2008.12.04
*
*/
class Crypt3Des
{ 
public $key = "01234567890123456789012345678912";
public $iv = "23456789"; //like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
//加密
public function encrypt($input)
{
$input = $this->padding( $input );
$key = base64_decode($this->key);
$td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
//使用MCRYPT_3DES算法,cbc模式
mcrypt_generic_init($td, $key, $this->iv);
//初始处理
$data = mcrypt_generic($td, $input);
//加密
mcrypt_generic_deinit($td);
//结束
mcrypt_module_close($td);
$data = $this->removeBR(base64_encode($data));
return $data;
}
//解密
public function decrypt($encrypted)
{
$encrypted = base64_decode($encrypted);
$key = base64_decode($this->key);
$td = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,'');
//使用MCRYPT_3DES算法,cbc模式
mcrypt_generic_init($td, $key, $this->iv);
//初始处理
$decrypted = mdecrypt_generic($td, $encrypted);
//解密
mcrypt_generic_deinit($td);
//结束
mcrypt_module_close($td);
$decrypted = $this->removePadding($decrypted);
return $decrypted;
}
//填充密码,填充至8的倍数
public function padding( $str )
{
$len = 8 - strlen( $str ) % 8;
for ( $i = 0; $i < $len; $i++ )
{
$str .= chr( 0 );
}
return $str ;
}
//删除填充符
public function removePadding( $str )
{
$len = strlen( $str );
$newstr = "";
$str = str_split($str);
for ($i = 0; $i < $len; $i++ )
{
if ($str[$i] != chr( 0 ))
{
$newstr .= $str[$i];
}
}
return $newstr;
}
//删除回车和换行
public function removeBR( $str ) 
{
$len = strlen( $str );
$newstr = "";
$str = str_split($str);
for ($i = 0; $i < $len; $i++ )
{
if ($str[$i] != '\n' and $str[$i] != '\r')
{
$newstr .= $str[$i];
}
}
return $newstr;
}
}
//test
$input = "1qaz2ws";
echo "plainText:" . $input."<br/>";
$crypt = new Crypt3Des();
echo "Encode:".$crypt->encrypt($input)."<br/>";
echo "Decode:".$crypt->decrypt($crypt->encrypt($input));
&#63;>
Copy after login

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template