1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| namespace libs;
class Aes{ const CIPHER = MCRYPT_DES; const MODE = MCRYPT_MODE_ECB;
public static function encode($str,$key){ $size = mcrypt_get_block_size ( MCRYPT_DES, 'ecb' ); $str = self::pkcs5_pad($str, $size); $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER,self::MODE),MCRYPT_RAND); $result = mcrypt_encrypt(self::CIPHER, $key, $str, self::MODE, $iv); return base64_encode($result); }
public static function decode($str,$key){ $str = base64_decode($str); $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER,self::MODE),MCRYPT_RAND); $str = trim(mcrypt_decrypt(self::CIPHER, $key, $str, self::MODE, $iv)); return self::pkcs5_unpad($str); }
private static function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); }
private 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); } }
|