PHP AES Encryption Learning notes

Source: Internet
Author: User
PHP AES Encryption Learning notes

?

?

 
  ';      printf ("128-bit encrypted result:\n%s\n\n", Bin2Hex ($cipherText));      Echo '
'; Echo '
'; } --------the first type of AES encryption scheme--------
Reprint Source: http://www.chilkatsoft.com/p/php_aes.asphttp://www.cnblogs.com/adylee/archive/2007/09/14/893438. HTML Reprint Source: http://blog.csdn.net/shushengsky/archive/2009/12/13/4961861.aspx
 ';  $key = ' 1234567890123456 ';  $content = ' Hello ';  $padkey = Pad2length ($key, 16);  $cipher = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_ECB, ');  $iv _size = mcrypt_enc_get_iv_size ($cipher); $iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);  #IV自动生成? Echo ' automatically generates the length of IV: '. strlen ($IV). ' Bit: '. Bin2Hex ($IV). '
'; if (Mcrypt_generic_init ($cipher, Pad2length ($key, +), $IV)! =-1) {//PHP pads with NULL bytes If $content are not a Multiple of the block size. $cipherText = Mcrypt_generic ($cipher, Pad2length ($content, 16)); Mcrypt_generic_deinit ($cipher); Mcrypt_module_close ($cipher); Display the result in hex. printf ("128-bit encrypted result:\n%s\n\n", Bin2Hex ($cipherText)); Print ("
"); }//Decryption $MW = Bin2Hex ($cipherText); $TD = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_ECB, '); if (Mcrypt_generic_init ($TD, $padkey, $iv)! =-1) {$p _t = mdecrypt_generic ($TD, Hextostr ($MW)); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); $p _t = trimEnd ($p _t); Echo ' decryption: '; Print ($p _t); Print ("
"); Print (Bin2Hex ($p _t)); Echo '

'; }//Will $text complement $padlen multiplier length function pad2length ($text, $padlen) {$len = strlen ($text)% $padlen; $res = $text; $span = $padlen-$len; for ($i =0; $i < $span; $i + +) {$res. = Chr ($span); } return $res; }//Remove the extra length after decryption (because the length of the block_size is added at the time of encryption) function TrimEnd ($text) {$len = strlen ($text); $c = $text [$len-1]; if (Ord ($c) < $len) {for ($i = $len-ord ($c); $i < $len; $i + +) {if ($text [$i]! = $c) { return $text; }} return substr ($text, 0, $len-ord ($c)); } return $text; }//16 binary to 2 binary string function Hextostr ($hex) {$bin = ""; for ($i =0; $i
  ';  $key = ' 1234567890123456 ';  $key = Pad2length ($key, 16);  $iv = ' ASDFF ';  $content = ' Hello ';  $content = Pad2length ($content, 16); $AESed = Bin2Hex (Mcrypt_encrypt (mcrypt_rijndael_128, $key, $content, MCRYPT_MODE_ECB, $iv)); #加密 echo "128-bit encrypted Result:" $AESed. '  
'; $jiemi = Mcrypt_decrypt (mcrypt_rijndael_128, $key, Hextostr ($AESed), MCRYPT_MODE_ECB, $IV); #解密 echo ' decryption: '; echo trimEnd ($jiemi); --------the third AES encryption scheme--------? > #KEY长度无限制, iv length must be a multiple of block_size # if it is MCRYPT_MODE_ECB encryption, the result is related to the key, and IV is irrelevant if it is mcrypt_ MODE_CBC encryption, the result is related to key, and IV is related to
?

?

?

?

  • 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: info-contact@alibabacloud.com 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.