I am also new to PHP. I started writing it after learning about the basic principles of PHP through w3cschool. But still a rookie.
Regardless of whether the 3DES encryption method is correct or not, the methods are all online. When running, an error was reported, which killed me. After much searching, I finally found a way.
<?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)); ?>
You don’t have to read the code, just look at the sentence inside: $td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); He was the one who reported the error.
I searched for a lot of solutions, and the correct method should be (only for Windows systems):
This error will occur when the server running php lacks libmcrypt.dll and uses the function mcrypt_module_open to decrypt.
Make the following settings on the server to solve the problem.
Go to the Internet to download a PHP mcrypt module installation package. You only need the libmcrypt.dll file (usually downloaded from the official website and already in the PHP directory)
1. Copy libmcrypt.dll to the system32 directory or PHP In the extensions directory under the installation directory
2. Copy libmcrypt.dll to the bin directory in the apache installation directory
3. Find the php.ini file in the windows directory and open it
4. Find; Directory in which the loadable extensions (modules) reside.
extension_dir = "./" For example: extension_dir = "D:php5ext"
These two lines must make libmcrypt.dll found in the directory pointed by extension_dir, or in the system path There is libmcrypt.dll
5. Find the line ;extension=php_mcrypt.dll under the Windows Extensions item and the line ;extension=php_iconv.dll (I don’t have it, so I omitted it), and remove the preceding semicolon
ps: I just started looking at the solutions online. Some said to modify php.ini in the php installation directory, but it was useless after modification. Be sure to modify php.ini in the windows directory!
The above introduces how to solve the PHP encryption 3DES error Call to undefined function: mcrypt_module_open, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.