This article mainly introduced the implementation of PHP AES encryption class definition and usage, combined with a complete instance of the AES encryption class based on the implementation and use of the method, the need for friends can refer to the following
This paper describes the definition and usage of AES encryption class implemented by PHP. Share to everyone for your reference, as follows:
CryptAES.class.php file:
<?phpclass cryptaes{protected $cipher = mcrypt_rijndael_128; protected $mode = MCRYPT_MODE_ECB; protected $pad _method = NULL; protected $secret _key = "; protected $iv = "; Public Function Set_cipher ($cipher) {$this->cipher = $cipher,} public Function Set_mode ($mode) {$this->mode = $mo De Public Function Set_iv ($iv) {$this->iv = $iv,} public Function Set_key ($key) {$this->secret_key = $key;} publi C function Require_pkcs5 () {$this->pad_method = ' pkcs5 ';} protected function Pad_or_unpad ($STR, $ext) {if (Is_null ( $this->pad_method)) {return $str;} else {$func _name = __class__. '::' . $this->pad_method. '_' . $ext. ' Pad '; if (is_callable ($func _name)) {$size = Mcrypt_get_block_size ($this->cipher, $this->mode); Return Call_user_func ($func _name, $str, $size); }} return $str; } protected function Pad ($str) {return $this->pad_or_unpad ($str, '),} protected function Unpad ($STR) {return $this- >pad_or_unpad ($str, ' un '); } publiC function Encrypt ($STR) {$str = $this->pad ($str); $td = Mcrypt_module_open ($this->cipher, ", $this->mode,"); if (Empty ($this->iv)) {$iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand),} else {$iv = $this-&G T;iv; } mcrypt_generic_init ($TD, $this->secret_key, $IV); $cyper _text = Mcrypt_generic ($TD, $STR); $rt =base64_encode ($cyper _text); $rt = Bin2Hex ($cyper _text); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); return $rt; Public function Decrypt ($str) {$td = Mcrypt_module_open ($this->cipher, "", $this->mode, "); if (Empty ($this IV) {$IV = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand),} else {$iv = $this->iv;} mcrypt_generic _init ($TD, $this->secret_key, $IV); $decrypted _text = Mdecrypt_generic ($TD, Self::hex2bin ($STR)); $decrypted _text = Mdecrypt_generic ($TD, Base64_decode ($STR)); $rt = $decrypted _text; Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); return $this->unpad ($RT); } Public Static function Hex2bin ($hexdata) {$bindata = "; $length = strlen ($hexdata); for ($i =0; $i < $length; $i + = 2) {$bind Ata. = Chr (Hexdec (substr ($hexdata, $i, 2))); } return $bindata; public static function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); return $text. s Tr_repeat (Chr ($pad), $pad); public static function Pkcs5_unpad ($text) {$pad = Ord ($text {strlen ($text)-1}), if ($pad > strlen ($text)) return FA Lse if (strspn ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) return false; Return substr ($text, 0,-1 * $pad); }}?>
Usage:
Require_once ("CryptAES.class.php"); $keyStr = ' Ss4fs4skfhksk '; $aes = new Cryptaes (); $keyStr = $aes->hex2bin ($keyStr ); $aes->set_key ($KEYSTR); $aes->require_pkcs5 (); $d = $aes->encrypt ($data);
Note: this needs to be opened in php.ini:extension=php_mcrypt.dll
PS: About encryption and decryption of interested friends can also refer to the site online tools:
Text online encryption and decryption tool (includes AES, DES, RC4, etc.):
Http://tools.jb51.net/password/txt_encode
MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password
Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt
Online md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160 Encryption Tool:
Http://tools.jb51.net/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
Http://tools.jb51.net/password/sha_encode
Articles you may be interested in:
PHP implements PHP tips for preventing cross-site and XSS attack code
How to install YAF extensions on Ubuntu PHP instance
thinkphp3.2.0 Setinc method source code comprehensive analysis PHP instance