PHP AES Encryption algorithm examples of detailed

Source: Internet
Author: User
Tags mcrypt
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.