PHP encryption 3DES error Call to undefined function: mcrypt_module_open () solution, mcryptmoduleopen
I am also a beginner in PHP. After learning about the basic principles of php through w3cschool, I started writing. But it is still a cainiao.
No matter whether the 3DES encryption method is correct or not, the method is online. When running, an error is reported, killing the younger brother. Finally, I found a method.
<? Php/*** PHP version 3DES encryption/Decryption class ** can be compatible with java's 3DES (DESede) encryption method ** @ 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}; // encrypt public function encrypt ($ input) {$ input = $ this-> padding ($ input); $ key = base64_decode ($ this-> key ); $ Td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_CBC,''); // use the MCRYPT_3DES algorithm, cbc mode mcrypt_generic_init ($ td, $ key, $ this-> iv); // initial processing $ data = mcrypt_generic ($ td, $ input); // encrypt mcrypt_generic_deinit ($ td ); // end mcrypt_module_close ($ td); $ data = $ this-> removeBR (base64_encode ($ data); return $ data;} // decrypt public function decrypt ($ encrypted) {$ encrypted = base64_decode ($ encrypted); $ key = base 64_decode ($ this-> key); $ td = mcrypt_module_open (MCRYPT_3DES, '', encryption,''); // use the MCRYPT_3DES algorithm, cbc mode mcrypt_generic_init ($ td, $ key, $ this-> iv); // initial processing $ decrypted = mdecrypt_generic ($ td, $ encrypted); // decrypt mcrypt_generic_deinit ($ td ); // end mcrypt_module_close ($ td); $ decrypted = $ this-> removePadding ($ decrypted); return $ decrypted;} // fill in the password, fill in the multiple public function padding ($ str) {$ len = 8-s Trlen ($ str) % 8; for ($ I = 0; $ I <$ len; $ I ++) {$ str. = chr (0);} return $ str;} // Delete the padding character 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;} // Delete the carriage return and line feed 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);?>
If you do not want to check the code, you can see the following sentence: $ td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_CBC,''); the error is reported.
I searched for a lot of solutions. The correct method should be (for windows only ):
This error occurs when you use the mcrypt_module_open function to decrypt libmcrypt. dll on the server that runs php.
Make the following settings on the server to solve the problem.
Download a php mcrypt module installation package from the Internet. You only need the libmcrypt. dll file.(The php Directory already exists on the official website)
1. Copy libmcrypt. dll to the system32 directory or the extensions directory under the php installation directory.
2. Copy libmcrypt. dll to the bin directory of the apache installation directory.
3.Find the php. ini file in the windows directory., Open it
4. Find; Directory in which the loadable extensions (modules) reside.
Extension_dir = "./" For example: extension_dir = "D: \ php5 \ ext"
In these two rows, you need to find libmcrypt. dll in the directory pointed to by extension_dir, or libmcrypt. dll in the system path.
5. Find the extension = php_mcrypt.dll line and extension = php_iconv.dll line under Windows Extensions, and remove the semicolon
Ps: At the beginning, I started to look at the solution on the Internet. Some said that it was useless to modify the php. ini under the php installation directory. Make sure to modify php. ini in the windows directory!