本文实例讲述了PHP实现的AES加密、解密封装类与用法。分享给大家供大家参考,具体如下:
<"mcrypt")) { // throw new \Exception("mcrypt extension do not exist. it was DEPRECATED in PHP 7.1.0, and REMOVED in PHP 7.2.0. use OpenSSL instead"); } $this->key = $key; $this->iv = $iv; } /** * 加密数据 * @param $data * * @return string */ public function encrypt($data) { $td = mcrypt_module_open($this->cipher, '', $this->mode, ''); $key = hash("sha256", $this->key, true); $iv = isset($this->iv) "sha256", $this->iv, true) : $key; $data = $this->padding($data); mcrypt_generic_init($td, $key, $iv); $encryptedData = base64_encode(mcrypt_generic($td, $data)); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $encryptedData; } /** * 解密数据 * @param $data * * @return bool|string */ public function decrypt($data) { $td = mcrypt_module_open($this->cipher, '', $this->mode, ''); $key = hash("sha256", $this->key, true); $iv = isset($this->iv) "sha256", $this->iv, true) : $key; mcrypt_generic_init($td, $key, $iv); $decrypted_data = mdecrypt_generic($td, base64_decode($data)); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $this->unPadding($decrypted_data); } /** * 填充数据到分组大小的整数倍 * @param null $data * * @return string */ protected function padding($data = null) { $blockSize = 32; //MCRYPT_RIJNDAEL_256算法的分组大小是32字节 $pad = $blockSize - (strlen($data) % $blockSize); return $data . str_repeat(chr($pad), $pad); } /** * 去掉填充的数据 * @param null $data * * @return bool|string */ protected function unPadding($data = null) { $pad = ord($data[strlen($data) - 1]); if ($pad > strlen($data)) { return false; } if (strspn($data, chr($pad), strlen($data) - $pad) != $pad) { return false; } return substr($data, 0, -1 * $pad); } /** * @return mixed */ public function getSecretKey() { return $this->key; } /** * @param mixed $key */ public function setSecretKey($key) { $this->key = $key; } /** * @return null */ public function getIv() { return $this->iv; } /** * @param null $iv */ public function setIv($iv) { $this->iv = $iv; } } //使用方法: $keyStr = 'sq8f77fwhksk'; $aes = new AES($keyStr); $str = 'www.jb51.net'; $chgstr = $aes->encrypt($str); echo $chgstr; echo "<br/>"; $rstr = $aes->decrypt($chgstr); echo $rstr; "color: #800000">PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encodeMD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode更多关于PHP相关内容感兴趣的读者可查看本站专题:《php加密方法总结》、《PHP编码与转码操作技巧汇总》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php正则表达式用法总结》
希望本文所述对大家PHP程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 何耀珊.2006-收获【华纳】【WAV+CUE】
- 梁汉文1994-壹精选[华星][WAV+CUE]
- 洪荣宏1981-歹路不可行·相思雨(飞跃复刻版)[光美][WAV+CUE]
- 旅行团2015-10Day’s[来福胶泥][WAV+CUE]
- 龙千玉.2011-卡将哟【豪记】【WAV+CUE】
- 蔣志光.1993-多情多寂寞(国专)【BMG】【WAV+CUE】
- 陈慧娴.1996-雪映美白96演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 王子异《1996 Part.2》[FLAC/分轨][50.66MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[320K/MP3][155.91MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[FLAC/分轨][251.78MB]
- 黄韵玲2001-巴结音乐[全员集合][WAV+CUE]
- 郑绪岚2015-流金岁月·精选[首版][WAV+CUE]
- 周冰倩1995《沧桑情歌》JVC日本录制[WAV+CUE]
- 辛禄乐队.2024-热烈的少年【星辰】【FLAC分轨】
- 成龙.1996-龙的心【滚石】【WAV+CUE】