Introduction: This is a detailed page of php aes encryption learning notes. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 339597 'rolling = 'no'>
<? PHP // -------- first AES-CBC encryption scheme -------- // for understanding only $ cipher = mcrypt_module_open (mcrypt_rijndael_128, '', mcrypt_mode_cbc ,''); #128 bits = 16-byte IV must be 16 bytes $ key128 = '000000'; $ IV = '000000'; $ cleartext = 'hello '; # If (mcrypt_generic_init ($ cipher, $ key128, $ IV) string to be encrypted )! =-1) {// PHP pads with NULL bytes if $ cleartext is not a multiple of the block size .. // If $ cleartext is neither 128-bit nor a multiple of 16 bytes, the length of the returned result is the same as that of null. $ ciphertext = mcrypt_generic ($ cipher, $ cleartext ); mcrypt_generic_deinit ($ cipher); // display the result in hex. // obviously, the result is a multiple of 16 bytes. one byte is expressed in two hexadecimal notation. Therefore, the following output is a 32-bit hexadecimal string echo 'first AES Encryption Scheme: <br> '; printf ("128-bit encrypted result: \ n % s \ n", bin2hex ($ ciphertext )); Echo '<br>';} // -------- first AES encryption solution --------?> </PRE> Source: External <PRE lang = "php"> <? PHP // -------- second AES-ECB encryption scheme -------- // encrypted echo 'second AES Encryption Scheme: <br> '; $ key = '000000'; $ content = 'hello '; $ padkey = pad2length ($ key, 16); $ cipher = mcrypt_module_open (mcrypt_rijndael_128, '', mcrypt_mode_ecb,''); $ iv_size = hour ($ cipher ); $ IV = mcrypt_create_iv ($ iv_size, mcrypt_rand); # Is IV automatically generated? Echo 'length of automatically generated IV :'. strlen ($ IV ). 'bit :'. bin2hex ($ IV ). '<br>'; If (mcrypt_generic_init ($ cipher, pad2length ($ key, 16), $ IV )! =-1) {// PHP pads with NULL bytes if $ content is 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", bin2hex ($ ciphertext); print ("<br/> ");} // decrypt $ 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 ("<br/>"); print (bin2hex ($ p_t )); echo '<br>';} // Add $ text to the length function pad2length ($ text, $ padlen) in multiples of $ 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 block_size is added during 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 ;} // convert the hexadecimal string to the binary string function hextostr ($ HEX) {$ bin = ""; for ($ I = 0; $ I <strlen ($ HEX) -1; $ I + = 2) {$ bin. = CHR (hexdec ($ hex [$ I]. $ hex [$ I + 1]);} return $ bin;} // -------- second AES encryption solution -------- </PRE> <PRE lang = "php"> <? PHP // -------- third AES-ECB encryption scheme -------- echo 'third AES Encryption Scheme: <br> '; $ key = '000000'; $ 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); # encrypt echo "128-bit encrypted Result :". $ aesed. '<br>'; $ jiemi = mcrypt_decrypt (mcrypt_rijndael_128, $ key, hextostr ($ aesed), mcryp T_mode_ecb, $ IV); # decrypt echo 'decryption: '; echo trimend ($ jiemi); // -------- third AES encryption solution --------?> # The key length is not limited. The IV length must be a multiple of block_size. # If it is mcrypt_mode_ecb encryption, the result is related to the key. # If it is mcrypt_mode_cbc encryption, the result is related to the key and IV.
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/339597.html pageno: 7.