phpaes.class.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. define('PHPAES_ROOT', dirname(__FILE__));
  3. include PHPAES_ROOT . '/phpseclib/Crypt/Common/BlockCipher.php';
  4. include PHPAES_ROOT . '/phpseclib/Crypt/AES.php';
  5. include PHPAES_ROOT . '/phpseclib/Crypt/Random.php';
  6. class phpAES
  7. {
  8. CONST MODE_CTR = -1;
  9. CONST MODE_ECB = 1;
  10. CONST MODE_CBC = 2;
  11. const MODE_CFB = 3;
  12. const MODE_OFB = 4;
  13. const MODE_STREAM = 5;
  14. const ENGINE_INTERNAL = 1;
  15. const ENGINE_EVAL = 2;
  16. const ENGINE_MCRYPT = 3;
  17. const ENGINE_OPENSSL = 4;
  18. public function init($key, $iv)
  19. {
  20. $this->aes = new AES(self::MODE_CBC);
  21. $this->aes->setKey($key);
  22. $this->aes->setIV($iv);
  23. }
  24. public function encrypt($output)
  25. {
  26. return $this->aes->encrypt($output);
  27. }
  28. public function decrypt($input)
  29. {
  30. return $this->aes->decrypt($input);
  31. }
  32. public function getEngine()
  33. {
  34. switch($this->aes->getEngine())
  35. {
  36. case self::ENGINE_INTERNAL : return 'ENGINE_INTERNAL';
  37. case self::ENGINE_EVAL : return 'ENGINE_EVAL';
  38. case self::ENGINE_MCRYPT : return 'ENGINE_MCRYPT';
  39. case self::ENGINE_OPENSSL : return 'ENGINE_OPENSSL';
  40. default: return 'error';
  41. }
  42. }
  43. public static function randomString($length)
  44. {
  45. return Random::string($length);
  46. }
  47. }