欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

AES

程序员文章站 2022-05-10 23:06:13
...
  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