- <?php
- $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); #128位 = 16字节 iv必须16字节
- $key128 = '1234567890123456';
- $iv = '1234567890123456';
- $cleartext = 'hello'; #待加密的字符串
- if (mcrypt_generic_init($cipher, $key128, $iv) != -1)
- {
-
-
- $cipherText = mcrypt_generic($cipher,$cleartext );
- mcrypt_generic_deinit($cipher);
-
-
-
- echo '第一种AES加密方案:<br>';
- printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));
- echo '<br>';echo '<br>';
- }
- ?>
- </pre>
- 转载来源:http:
-
- http:
-
- 转载来源:http:
- <pre lang="php">
- <?php
- echo '第二种AES加密方案:<br>';
- $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).'<br>';
- if (mcrypt_generic_init($cipher, pad2Length($key,16), $iv) != -1)
- {
-
- $cipherText = mcrypt_generic($cipher,pad2Length($content,16) );
- mcrypt_generic_deinit($cipher);
- mcrypt_module_close($cipher);
-
-
- printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));
- print("<br />");
-
- }
- $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("<br />");
- print(bin2hex($p_t));
- echo '<br><br>';
- }
- function pad2Length($text, $padlen){
- $len = strlen($text)%$padlen;
- $res = $text;
- $span = $padlen-$len;
- for($i=0; $i<$span; $i++){
- $res .= chr($span);
- }
- return $res;
- }
- 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;
- }
- function hexToStr($hex)
- {
- $bin="";
- for($i=0; $i<strlen($hex)-1; $i+=2)
- {
- $bin.=chr(hexdec($hex[$i].$hex[$i+1]));
- }
- return $bin;
- }
- </pre>
- <pre lang="php">
- <?php
- echo '第三种AES加密方案:<br>';
- $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.'<br>';
- $jiemi = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$key,hexToStr($AESed),MCRYPT_MODE_ECB,$iv); #解密
- echo '解密:';
- echo trimEnd($jiemi);
- ?>
-
- #KEY长度无限制,IV长度必须是block_size的倍数
- #如果是MCRYPT_MODE_ECB加密,结果与KEY有关,与IV无关
- #如果是MCRYPT_MODE_CBC加密,结果与KEY有关,与IV有关
-
- function ecryptdString($str,$keys="6461772803150152",$iv="8105547186756005",$cipher_alg=MCRYPT_RIJNDAEL_128){
- $encrypted_string= bin2hex(mcrypt_encrypt($cipher_alg,$keys, $str, MCRYPT_MODE_CBC,$iv));
- return$encrypted_string;
- }
- functiondecryptStrin($str,$keys="6461772803150152",$iv="8105547186756005",$cipher_alg=MCRYPT_RIJNDAEL_128){
- $decrypted_string= mcrypt_decrypt($cipher_alg,$keys, pack("H*",$str),MCRYPT_MODE_CBC,$iv);
- return$decrypted_string;
- }
转载于:https://www.cnblogs.com/huojing/articles/4166733.html