This article mainly introduces the PHP AES encryption algorithm, combined with a complete example of the PHP AES encryption algorithm class and its specific usage, with a certain reference value, the need for friends can refer to the following
Specific as follows:
<?phpclass MCrypt {private $hex _iv = ' 00000000000000000000000000000000 '; # converted JAVA byte code in to hex and PLA CED it here private $key = ' U1MJU1M0FDOUZ.QZ '; #Same as in JAVA function __construct () {$this->key = hash (' sha256 ', $this->key, True); echo $this->key. ' <br/> '; } function Encrypt ($str) {$td = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_CBC, '); Mcrypt_generic_init ($TD, $this->key, $this->hextostr ($this->hex_iv)); $block = Mcrypt_get_block_size (mcrypt_rijndael_128, MCRYPT_MODE_CBC); $pad = $block-(strlen ($STR)% $block); $str. = Str_repeat (Chr ($pad), $pad); $encrypted = Mcrypt_generic ($TD, $STR); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); Return Base64_encode ($encrypted); } function Decrypt ($code) {$td = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_CBC, '); Mcrypt_generic_init ($TD, $this->key, $this->hextostr ($this->hex_iv)); $str = Mdecrypt_genEric ($td, Base64_decode ($code)); $block = Mcrypt_get_block_size (mcrypt_rijndael_128, MCRYPT_MODE_CBC); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); return $this->strippadding ($STR); }/* for PKCS7 padding */Private Function addpadding ($string, $blocksize = +) {$len = strlen ($string); $pad = $blocksize-($len% $blocksize); $string. = Str_repeat (Chr ($pad), $pad); return $string; } Private Function Strippadding ($string) {$slast = Ord (substr ($string,-1)); $SLASTC = Chr ($slast); $pcheck = substr ($string,-$slast); if (Preg_match ("/$SLASTC {"). $slast. "}/", $string)) {$string = substr ($string, 0, strlen ($string)-$slast); return $string; } else {return false; }} function Hextostr ($hex) {$string = '; for ($i =0; $i < strlen ($hex)-1; $i +=2) {$string. = Chr (Hexdec ($hex [$i]. $hex [$i +1]); } return $string; }} $encryption = new MCrypt (); Echo $encryption->encrypt (' 123456 '). "<bR/> "; Echo $encryption->decrypt (' tpyxisj83dqes3uw8bn/+w== ');? >
Summary: The above is the entire content of this article, I hope to be able to help you learn.