AES

WBOY
发布: 2016-07-25 08:43:00
原创
934 人浏览过
  1. class aes {
  2. static public $mode = MCRYPT_MODE_NOFB;
  3. static public function generateKey($length=32) {
  4. if (!in_array($length,array(16,24,32)))
  5. return False;
  6. $str = '';
  7. for ($i=0;$i $str .= chr(rand(33,126));
  8. }
  9. return $str;
  10. }
  11. static public function encrypt($data, $key) {
  12. if (strlen($key) > 32 || !$key)
  13. return trigger_error('key too large or key is empty.', E_USER_WARNING) && False;
  14. $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, self::$mode);
  15. $iv = mcrypt_create_iv($ivSize, (substr(PHP_OS,0,1) == 'W' ? MCRYPT_RAND : MCRYPT_DEV_URANDOM ));
  16. $encryptedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, self::$mode, $iv);
  17. $encryptedData = $iv . $encryptedData;
  18. return base64_encode($encryptedData);
  19. }
  20. static public function decrypt($data, $key) {
  21. if (strlen($key) > 32 || !$key)
  22. return trigger_error('key too large or key is empty.', E_USER_WARNING) && False;
  23. $data = base64_decode($data);
  24. if (!$data)
  25. return False;
  26. $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, self::$mode);
  27. $iv = substr($data, 0, $ivSize);
  28. $data = substr($data, $ivSize);
  29. $decryptData = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, self::$mode, $iv);
  30. return $decryptData;
  31. }}
复制代码

AES


相关标签:
aes
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板