AES

WBOY
Release: 2016-07-25 08:43:00
Original
937 people have browsed it
  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<$length;$i++) {
  8. $str .= chr(rand(33,126));
  9. }
  10. return $str;
  11. }
  12. static public function encrypt($data, $key) {
  13. if (strlen($key) > 32 || !$key)
  14. return trigger_error('key too large or key is empty.', E_USER_WARNING) && False;
  15. $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, self::$mode);
  16. $iv = mcrypt_create_iv($ivSize, (substr(PHP_OS,0,1) == 'W' ? MCRYPT_RAND : MCRYPT_DEV_URANDOM ));
  17. $encryptedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, self::$mode, $iv);
  18. $encryptedData = $iv . $encryptedData;
  19. return base64_encode($encryptedData);
  20. }
  21. static public function decrypt($data, $key) {
  22. if (strlen($key) > 32 || !$key)
  23. return trigger_error('key too large or key is empty.', E_USER_WARNING) && False;
  24. $data = base64_decode($data);
  25. if (!$data)
  26. return False;
  27. $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, self::$mode);
  28. $iv = substr($data, 0, $ivSize);
  29. $data = substr($data, $ivSize);
  30. $decryptData = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, self::$mode, $iv);
  31. return $decryptData;
  32. }}
复制代码

AES


Related labels:
aes
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template