mrkirby153

Untitled

May 1st, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <?php
  2.  
  3. class Encryption {
  4.  
  5. const
  6. CYPHER = MCRYPT_RIJNDAEL_128,
  7. MODE = 'cbc',
  8. SALT = '';
  9.  
  10. /**
  11. * Encrypt string using mcrypt module
  12. * @param string $plaintext Text to be encrypted
  13. * @param string $password User entered password
  14. */
  15. public static function encrypt($plaintext, $password = '') {
  16. // $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
  17. // $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  18. // $key = self::SALT . trim($password);
  19. // mcrypt_generic_init($td, $key, $iv);
  20. // $crypttext = mcrypt_generic($td, $plaintext);
  21. // mcrypt_generic_deinit($td);
  22. // $crypttext = $iv . $crypttext;
  23. // return $crypttext;
  24. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $password, self::pkcs5_pad($plaintext), MCRYPT_MODE_ECB);
  25. }
  26.  
  27. /**
  28. * Decrypt string using mcrypt module
  29. * @param string $crypttext Text to be decrypted
  30. * @param string $password User entered password
  31. */
  32. public static function decrypt($crypttext, $password = '') {
  33. $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
  34. $ivsize = mcrypt_enc_get_iv_size($td);
  35. $iv = substr($crypttext, 0, $ivsize);
  36. $crypttext = substr($crypttext, $ivsize);
  37. $key = self::SALT . trim($password);
  38. mcrypt_generic_init($td, $key, $iv);
  39. return mdecrypt_generic($td, $crypttext);
  40. }
  41.  
  42. public static function pkcs5_pad($text, $blocksize = 16) {
  43. $pad = $blocksize - (strlen($text) % $blocksize);
  44. return $text . str_repeat(chr($pad), $pad);
  45. }
  46.  
  47. public static function pkcs5_unpad($text) {
  48. $pad = ord($text{strlen($text) - 1});
  49. if ($pad > strlen($text))
  50. return false;
  51. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
  52. return false;
  53. return substr($text, 0, -1 * $pad);
  54. }
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment