您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 济宁分类信息网,免费分类信息发布

PHP的AES加密算法实例详解

2025/9/29 6:11:00发布26次查看
这篇文章主要介绍了php的aes加密算法,结合完整实例形式分析了php的aes加密算法类及其具体用法,具有一定参考借鉴价值,需要的朋友可以参考下
具体如下:
<?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加密算法实例详解的详细内容。
济宁分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product