PHP AES加密解密学习(转)

  1. <?php      
  2. //--------第一种AES-CBC加密方案--------      
  3. //仅为理解之用      
  4. $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); #128位 = 16字节 iv必须16字节      
  5. $key128 = '1234567890123456';      
  6. $iv =  '1234567890123456';      
  7. $cleartext = 'hello'; #待加密的字符串      
  8. if (mcrypt_generic_init($cipher, $key128, $iv) != -1)      
  9. {      
  10.     // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..      
  11.     //如果$cleartext不是128位也就是16字节的倍数,补充NULL字符满足这个条件,返回的结果的长度一样      
  12.     $cipherText = mcrypt_generic($cipher,$cleartext );      
  13.     mcrypt_generic_deinit($cipher);      
  14.           
  15.     // Display the result in hex.      
  16.     //很明显,结果也是16字节的倍数.1个字节用两位16进制表示,所以下面输出的是32的倍数位16进制的字符串       
  17.     echo '第一种AES加密方案:<br>';      
  18.     printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));      
  19.     echo '<br>';echo '<br>';      
  20. }      
  21. //--------第一种AES加密方案--------      
  22. ?>     
  23. </pre>    
  24. 转载来源:http://www.chilkatsoft.com/p/php_aes.asp    
  25.     
  26. http://www.cnblogs.com/adylee/archive/2007/09/14/893438.html    
  27.     
  28. 转载来源:http://blog.csdn.net/shushengsky/archive/2009/12/13/4961861.aspx    
  29. <pre lang="php">    
  30. <?php      
  31. //--------第二种AES-ECB加密方案--------      
  32. //加密      
  33. echo '第二种AES加密方案:<br>';      
  34. $key = '1234567890123456';      
  35. $content = 'hello';      
  36. $padkey = pad2Length($key,16);      
  37. $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');      
  38. $iv_size = mcrypt_enc_get_iv_size($cipher);      
  39. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); #IV自动生成?      
  40. echo '自动生成iv的长度:'.strlen($iv).'位:'.bin2hex($iv).'<br>';      
  41. if (mcrypt_generic_init($cipher, pad2Length($key,16), $iv) != -1)      
  42. {      
  43.     // PHP pads with NULL bytes if $content is not a multiple of the block size..      
  44.     $cipherText = mcrypt_generic($cipher,pad2Length($content,16) );      
  45.     mcrypt_generic_deinit($cipher);      
  46.     mcrypt_module_close($cipher);      
  47.          
  48.     // Display the result in hex.      
  49.     printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));      
  50.     print("<br />");      
  51.          
  52. }      
  53. //解密      
  54. $mw = bin2hex($cipherText);      
  55. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');      
  56. if (mcrypt_generic_init($td, $padkey, $iv) != -1)      
  57. {      
  58.     $p_t = mdecrypt_generic($td, hexToStr($mw));      
  59.     mcrypt_generic_deinit($td);      
  60.     mcrypt_module_close($td);      
  61.          
  62.     $p_t = trimEnd($p_t);      
  63.     echo '解密:';      
  64.     print($p_t);      
  65.     print("<br />");      
  66.     print(bin2hex($p_t));      
  67.     echo '<br><br>';      
  68. }      
  69. //将$text补足$padlen倍数的长度      
  70. function pad2Length($text, $padlen){      
  71.     $len = strlen($text)%$padlen;      
  72.     $res = $text;      
  73.     $span = $padlen-$len;      
  74.     for($i=0; $i<$span; $i++){      
  75.         $res .= chr($span);      
  76.     }      
  77.     return $res;      
  78. }      
  79. //将解密后多余的长度去掉(因为在加密的时候 补充长度满足block_size的长度)      
  80. function trimEnd($text){      
  81.     $len = strlen($text);      
  82.     $c = $text[$len-1];      
  83.     if(ord($c) <$len){      
  84.         for($i=$len-ord($c); $i<$len; $i++){      
  85.             if($text[$i] != $c){      
  86.                 return $text;      
  87.             }      
  88.         }      
  89.         return substr($text, 0, $len-ord($c));      
  90.     }      
  91.     return $text;      
  92. }      
  93. //16进制的转为2进制字符串      
  94. function hexToStr($hex)       
  95. {       
  96.     $bin="";       
  97.     for($i=0; $i<strlen($hex)-1; $i+=2)       
  98.     {      
  99.         $bin.=chr(hexdec($hex[$i].$hex[$i+1]));       
  100.     }      
  101.     return $bin;       
  102. }      
  103. //--------第二种AES加密方案--------      
  104. </pre>    
  105. <pre lang="php">    
  106. <?php      
  107. //--------第三种AES-ECB加密方案--------      
  108. echo '第三种AES加密方案:<br>';      
  109. $key = '1234567890123456';      
  110. $key = pad2Length($key,16);      
  111. $iv = 'asdff';      
  112. $content = 'hello';      
  113. $content = pad2Length($content,16);      
  114. $AESed =  bin2hex( mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key,$content,MCRYPT_MODE_ECB,$iv) ); #加密      
  115. echo "128-bit encrypted result:".$AESed.'<br>';      
  116. $jiemi = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$key,hexToStr($AESed),MCRYPT_MODE_ECB,$iv); #解密      
  117. echo '解密:';      
  118. echo trimEnd($jiemi);      
  119. //--------第三种AES加密方案--------      
  120. ?>    
  121.     
  122. #KEY长度无限制,IV长度必须是block_size的倍数    
  123. #如果是MCRYPT_MODE_ECB加密,结果与KEY有关,与IV无关    
  124. #如果是MCRYPT_MODE_CBC加密,结果与KEY有关,与IV有关    



  1. //--------第四种AES加密/解密方案 CBC模式,128-bit--------   
    1. /* 
    2. * 实现AES加密 
    3. * $str : 要加密的字符串 
    4. * $keys : 加密密钥 
    5. * $iv : 加密向量 
    6. * $cipher_alg : 加密方式 
    7. */  
    8. function ecryptdString($str,$keys="6461772803150152",$iv="8105547186756005",$cipher_alg=MCRYPT_RIJNDAEL_128){  
    9.     $encrypted_string= bin2hex(mcrypt_encrypt($cipher_alg,$keys, $str, MCRYPT_MODE_CBC,$iv));  
    10.     return$encrypted_string;  
    11. }  
    12. /* 
    13. * 实现AES解密 
    14. * $str : 要解密的字符串 
    15. * $keys : 加密密钥 
    16. * $iv : 加密向量 
    17. * $cipher_alg : 加密方式 
    18. */  
    19. functiondecryptStrin($str,$keys="6461772803150152",$iv="8105547186756005",$cipher_alg=MCRYPT_RIJNDAEL_128){  
    20.     $decrypted_string= mcrypt_decrypt($cipher_alg,$keys, pack("H*",$str),MCRYPT_MODE_CBC,$iv);  
    21.     return$decrypted_string;  
    22. }  

posted on 2014-12-16 11:32  混元真人  阅读(321)  评论(0编辑  收藏  举报