博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP进行AES/ECB/PKCS7 padding加密的例子(mcrypt) (PHP版本小于7)
阅读量:3728 次
发布时间:2019-05-22

本文共 2411 字,大约阅读时间需要 8 分钟。

当php版本小于7时使用这个方法,当大于php7时使用

secret_key = $key; $this->pad_method =$method; $this->iv = $iv; $this->mode = $mode; $this->cipher = $cipher; } protected function pad_or_unpad($str, $ext) { if (!is_null($this->pad_method)) { $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad'; if (is_callable($func_name)) { $size = mcrypt_get_block_size($this->cipher, $this->mode); return call_user_func($func_name, $str, $size); } } return $str; } protected function pad($str) { return $this->pad_or_unpad($str, ''); } protected function unpad($str) { return $this->pad_or_unpad($str, 'un'); } public function encrypt($str) { $str = $this->pad($str); $td = mcrypt_module_open($this->cipher, '', $this->mode, ''); if (empty($this->iv)) { $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); } else { $iv = $this->iv; } mcrypt_generic_init($td, $this->secret_key, $iv); $cyper_text = mcrypt_generic($td, $str); $rt = base64_encode($cyper_text); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $rt; } public function decrypt($str) { $td = mcrypt_module_open($this->cipher, '', $this->mode, ''); if (empty($this->iv)) { $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); } else { $iv = $this->iv; } mcrypt_generic_init($td, $this->secret_key, $iv); $decrypted_text = mdecrypt_generic($td, base64_decode($str)); $rt = $decrypted_text; mcrypt_generic_deinit($td); mcrypt_module_close($td); return $this->unpad($rt); } public static function pkcs7_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } public static function pkcs7_unpad($text) { $pad = ord($text[strlen($text) - 1]); if ($pad > strlen($text)) return false; if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false; return substr($text, 0, -1 * $pad); }}$aes = new AES('aa2VN#N8DAL147L');echo $aes->encrypt('一片云资源站'), '
';// We2MEs6x595hkFvSwtS05g==echo $aes->decrypt('2LzvBMMOeEzMN7piI6xitg==');

 

转载地址:http://oqtnn.baihongyu.com/

你可能感兴趣的文章
2020-12-19 Vue-15CLI3 && 箭头函数
查看>>
2020-12-20L 闭包作用域 && 1431. 拥有最多糖果的孩子
查看>>
2020-12-21L this && 1656. 设计有序流(不知道啥意思)
查看>>
2020-12-21 Vue-15-router
查看>>
2020-12-22L 16.17. 连续数列 && splice
查看>>
2020-12-23L面试题 10.01. 合并排序的数组*** 不知道为啥错了
查看>>
2020-12-24L && 1051. 高度检查器
查看>>
2020-12-25L && 1380. 矩阵中的幸运数
查看>>
2020-12-26L地址栏location && 1389按照既定顺序创建目标数组 && 1413逐步求和得到正数的最小值
查看>>
2020-12-27L 5621. 无法吃午餐的学生数量 && 1502. 判断能否形成等差数列 && 1512. 好数对的数目
查看>>
2020-12-30 && 31 && 统计最大组数目 && this指向
查看>>
2021-01-05 斐波那契数列
查看>>
2021-01-08-补2021-1-8之前的 && 1399. 统计最大组的数目 && Map&&reduce
查看>>
2021-01-19补1.10-1.18
查看>>
2021-01-09 TabBar案例
查看>>
2021-01-20---1.24 &&L 最大子序列和-贪心法和动态规划法
查看>>
2021-01-22-Promise的简单使用
查看>>
2021-01-23-Vuex
查看>>
2021-01-25L1629&&1385
查看>>
2021-01-26---02-01L:830 && 448 && 1640 && 1646
查看>>