PHP加密3DES報錯 Call to undefined function: mcrypt_module_open() 的解決方案,mcryptmoduleopen_PHP教程

來源:互聯網
上載者:User

PHP加密3DES報錯 Call to undefined function: mcrypt_module_open() 的解決方案,mcryptmoduleopen


我也是PHP新手,通過w3cschool瞭解了一下php基本原理之後就開寫了。但仍是菜鳥。

先不管3DES加密的方法對不對,方法都是網上的,在啟動並執行時候報了個錯,把小弟整死了。找來找去終於自己摸出了方法。

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."
";$crypt = new Crypt3Des();echo "Encode:".$crypt->encrypt($input)."
";echo "Decode:".$crypt->decrypt($crypt->encrypt($input));?>

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

http://www.bkjia.com/PHPjc/1120376.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1120376.htmlTechArticlePHP加密3DES報錯 Call to undefined function: mcrypt_module_open() 的解決方案,mcryptmoduleopen 我也是PHP新手,通過w3cschool瞭解了一下php基本原理之後就開...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.