Security.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. class Security
  3. {
  4. public static function encrypt($input, $key) {
  5. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  6. $input = Security::pkcs5_pad($input, $size);
  7. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
  8. $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  9. mcrypt_generic_init($td, $key, $iv);
  10. $data = mcrypt_generic($td, $input);
  11. mcrypt_generic_deinit($td);
  12. mcrypt_module_close($td);
  13. $data = base64_encode($data);
  14. return $data;
  15. }
  16. private static function pkcs5_pad ($text, $blocksize) {
  17. $pad = $blocksize - (strlen($text) % $blocksize);
  18. return $text . str_repeat(chr($pad), $pad);
  19. }
  20. public static function decrypt($sStr, $sKey) {
  21. $decrypted= mcrypt_decrypt(
  22. MCRYPT_RIJNDAEL_128,
  23. $sKey,
  24. base64_decode($sStr),
  25. MCRYPT_MODE_ECB
  26. );
  27. $dec_s = strlen($decrypted);
  28. $padding = ord($decrypted[$dec_s-1]);
  29. $decrypted = substr($decrypted, 0, -$padding);
  30. return $decrypted;
  31. }
  32. }