PHP AES加密學習劄記

來源:互聯網
上載者:User
PHP AES加密學習筆記

?

?

';      printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));      echo '
';echo '
'; } //--------第一種AES加密方案-------- ?>
轉載來源:http://www.chilkatsoft.com/p/php_aes.asphttp://www.cnblogs.com/adylee/archive/2007/09/14/893438.html轉載來源: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 '自動產生iv的長度:'.strlen($iv).'位:'.bin2hex($iv).'
'; 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\n",bin2hex($cipherText)); print("
"); } //解密 $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 '解密:'; print($p_t); print("
"); print(bin2hex($p_t)); echo '

'; } //將$text補足$padlen倍數的長度 function pad2Length($text, $padlen){ $len = strlen($text)%$padlen; $res = $text; $span = $padlen-$len; for($i=0; $i<$span; $i++){ $res .= chr($span); } return $res; } //將解密後多餘的長度去掉(因為在加密的時候 補充長度滿足block_size的長度) 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進位的轉為2進位字串 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 '解密:'; echo trimEnd($jiemi); //--------第三種AES加密方案-------- ?>#KEY長度無限制,IV長度必須是block_size的倍數#如果是MCRYPT_MODE_ECB加密,結果與KEY有關,與IV無關#如果是MCRYPT_MODE_CBC加密,結果與KEY有關,與IV有關
?

?

?

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.