具体如下:
<?php class mcrypt { private $hex_iv = '00000000000000000000000000000000'; # converted java byte code in to hex and placed it here private $key = 'u1mju1m0fdouz.qz'; #same as in java function __construct() { $this->key = hash('sha256', $this->key, true); //echo $this->key.'<br/>'; } function encrypt($str) { $td = mcrypt_module_open(mcrypt_rijndael_128, '', mcrypt_mode_cbc, ''); mcrypt_generic_init($td, $this->key, $this->hextostr($this->hex_iv)); $block = mcrypt_get_block_size(mcrypt_rijndael_128, mcrypt_mode_cbc); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); $encrypted = mcrypt_generic($td, $str); mcrypt_generic_deinit($td); mcrypt_module_close($td); return base64_encode($encrypted); } function decrypt($code) { $td = mcrypt_module_open(mcrypt_rijndael_128, '', mcrypt_mode_cbc, ''); mcrypt_generic_init($td, $this->key, $this->hextostr($this->hex_iv)); $str = mdecrypt_generic($td, base64_decode($code)); $block = mcrypt_get_block_size(mcrypt_rijndael_128, mcrypt_mode_cbc); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $this->strippadding($str); } /* for pkcs7 padding */ private function addpadding($string, $blocksize = 16) { $len = strlen($string); $pad = $blocksize - ($len % $blocksize); $string .= str_repeat(chr($pad), $pad); return $string; } private function strippadding($string) { $slast = ord(substr($string, -1)); $slastc = chr($slast); $pcheck = substr($string, -$slast); if (preg_match("/$slastc{" . $slast . "}/", $string)) { $string = substr($string, 0, strlen($string) - $slast); return $string; } else { return false; } } function hextostr($hex) { $string=''; for ($i=0; $i < strlen($hex)-1; $i+=2) { $string .= chr(hexdec($hex[$i].$hex[$i+1])); } return $string; } } $encryption = new mcrypt(); echo $encryption->encrypt('123456') . "<br/>"; echo $encryption->decrypt('tpyxisj83dqes3uw8bn/+w=='); ?>
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php实现生成数据字典步骤详解
php框架codeigniter数据库配置步骤详解
php7的新特性使用详解
以上就是php的aes加密算法实例详解的详细内容。