SAASCryptAES.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. class SAASCryptAES
  3. {
  4. protected $cipher = MCRYPT_RIJNDAEL_128;
  5. protected $mode = MCRYPT_MODE_ECB;
  6. protected $pad_method = NULL;
  7. protected $secret_key = '';
  8. protected $iv = '';
  9. public function set_cipher($cipher)
  10. {
  11. $this->cipher = $cipher;
  12. }
  13. public function set_mode($mode)
  14. {
  15. $this->mode = $mode;
  16. }
  17. public function set_iv($iv)
  18. {
  19. $this->iv = $iv;
  20. }
  21. public function set_key($key)
  22. {
  23. $this->secret_key = $key;
  24. }
  25. public function require_pkcs5()
  26. {
  27. $this->pad_method = 'pkcs5';
  28. }
  29. protected function pad_or_unpad($str, $ext)
  30. {
  31. if ( is_null($this->pad_method) )
  32. {
  33. return $str;
  34. }
  35. else
  36. {
  37. $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
  38. if ( is_callable($func_name) )
  39. {
  40. $size = mcrypt_get_block_size($this->cipher, $this->mode);
  41. return call_user_func($func_name, $str, $size);
  42. }
  43. }
  44. return $str;
  45. }
  46. protected function pad($str)
  47. {
  48. return $this->pad_or_unpad($str, '');
  49. }
  50. protected function unpad($str)
  51. {
  52. return $this->pad_or_unpad($str, 'un');
  53. }
  54. public function encrypt($str)
  55. {
  56. $str = $this->pad($str);
  57. $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
  58. if ( empty($this->iv) )
  59. {
  60. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  61. }
  62. else
  63. {
  64. $iv = $this->iv;
  65. }
  66. mcrypt_generic_init($td, $this->secret_key, $iv);
  67. $cyper_text = mcrypt_generic($td, $str);
  68. $rt=base64_encode($cyper_text);
  69. //$rt = bin2hex($cyper_text);
  70. mcrypt_generic_deinit($td);
  71. mcrypt_module_close($td);
  72. return $rt;
  73. }
  74. public function decrypt($str){
  75. $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
  76. if ( empty($this->iv) )
  77. {
  78. $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  79. }
  80. else
  81. {
  82. $iv = $this->iv;
  83. }
  84. mcrypt_generic_init($td, $this->secret_key, $iv);
  85. //$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
  86. $decrypted_text = mdecrypt_generic($td, base64_decode($str));
  87. $rt = $decrypted_text;
  88. mcrypt_generic_deinit($td);
  89. mcrypt_module_close($td);
  90. return $this->unpad($rt);
  91. }
  92. /*
  93. public static function hex2bin($hexdata) {
  94. $bindata = '';
  95. $length = strlen($hexdata);
  96. for ($i=0; $i amp;< $length; $i += 2)
  97. {
  98. $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  99. }
  100. return $bindata;
  101. }
  102. */
  103. public static function pkcs5_pad($text, $blocksize)
  104. {
  105. $pad = $blocksize - (strlen($text) % $blocksize);
  106. return $text . str_repeat(chr($pad), $pad);
  107. }
  108. public static function pkcs5_unpad($text)
  109. {
  110. $pad = ord($text{strlen($text) - 1});
  111. if ($pad > strlen($text)) return false;
  112. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
  113. return substr($text, 0, -1 * $pad);
  114. }
  115. }
  116. ?>