Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Encryption {
- const
- CYPHER = MCRYPT_RIJNDAEL_128,
- MODE = 'cbc',
- SALT = '';
- /**
- * Encrypt string using mcrypt module
- * @param string $plaintext Text to be encrypted
- * @param string $password User entered password
- */
- public static function encrypt($plaintext, $password = '') {
- // $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
- // $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
- // $key = self::SALT . trim($password);
- // mcrypt_generic_init($td, $key, $iv);
- // $crypttext = mcrypt_generic($td, $plaintext);
- // mcrypt_generic_deinit($td);
- // $crypttext = $iv . $crypttext;
- // return $crypttext;
- return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $password, self::pkcs5_pad($plaintext), MCRYPT_MODE_ECB);
- }
- /**
- * Decrypt string using mcrypt module
- * @param string $crypttext Text to be decrypted
- * @param string $password User entered password
- */
- public static function decrypt($crypttext, $password = '') {
- $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
- $ivsize = mcrypt_enc_get_iv_size($td);
- $iv = substr($crypttext, 0, $ivsize);
- $crypttext = substr($crypttext, $ivsize);
- $key = self::SALT . trim($password);
- mcrypt_generic_init($td, $key, $iv);
- return mdecrypt_generic($td, $crypttext);
- }
- public static function pkcs5_pad($text, $blocksize = 16) {
- $pad = $blocksize - (strlen($text) % $blocksize);
- return $text . str_repeat(chr($pad), $pad);
- }
- public static function pkcs5_unpad($text) {
- $pad = ord($text{strlen($text) - 1});
- if ($pad > strlen($text))
- return false;
- if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
- return false;
- return substr($text, 0, -1 * $pad);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment