function reformMD5($pwd) { $totalStr = "JINRONGGONGCHANGAPPMD520141224INJINYUDASHAJINHONGDONGPRODUCT"; $pwd = strtoupper($pwd); $pwd = str_replace(range(0, 9), array('~', '$', '!', '@', ':', ']', '[', '{', '}', '`'), $pwd); $totalStr = substr($totalStr, 0, strlen($totalStr) - strlen($pwd)); $pwd = $totalStr . $pwd; return md5($pwd); } /* * AES 加密 * */ function encrypt($key, $value) { $padSize = 16 - (strlen($value) % 16); $value = $value . str_repeat(chr($padSize), $padSize); $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $value, MCRYPT_MODE_CBC, str_repeat(chr(0), 16)); return base64_encode($output); } /* * AES 解密 * */ function decrypt($key, $value) { $value = base64_decode($value); $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $value, MCRYPT_MODE_CBC, str_repeat(chr(0), 16)); $valueLen = strlen($output); if ($valueLen % 16 > 0) $output = ""; $padSize = ord($output{$valueLen - 1}); if (($padSize < 1) or ( $padSize > 16)) $output = ""; // Check padding. for ($i = 0; $i < $padSize; $i++) { if (ord($output{$valueLen - $i - 1}) != $padSize) $output = ""; } $output = substr($output, 0, $valueLen - $padSize); return $output; } /* * 生成36位随字符串 */ function makeKey() { return uniqid(uniqid(), true); } /* 生成验证码 * */ function makecode($code, $width = 80, $height = 28, $quality = 3) { $fontcfg = array('spacing' => 2); $fontfile ='../assets/font/ninab.ttf'; $fontcolors = array(array(27, 78, 181), array(22, 163, 35), array(214, 36, 7), array(88, 127, 30), array(66, 133, 244), array(241, 147, 0), array(232, 0, 0), array(196, 146, 1)); $im = imagecreatetruecolor($width * $quality, $height * $quality); $imbgcolor = imagecolorallocate($im, 255, 255, 255); imagefilledrectangle($im, 0, 0, $width * $quality, $height * $quality, $imbgcolor); $lettersMissing = 4 - strlen($code); $fontSizefactor = 0.9 + ($lettersMissing * 0.09); $x = 4 * $quality; $y = round(($height * 27 / 32) * $quality); $length = strlen($code); for ($i = 0; $i < $length; $i++) { $color = $fontcolors[mt_rand(0, sizeof($fontcolors) - 1)]; $imbgcolor = imagecolorallocate($im, $color[0], $color[1], $color[2]); $degree = rand(8 * -1, 8); $fontsize = 22 * $quality * $fontSizefactor; $letter = substr($code, $i, 1); $coords = imagettftext($im, $fontsize, $degree, $x, $y, $imbgcolor, $fontfile, $letter); $x += ($coords[2] - $x) + ($fontcfg['spacing'] * $quality); } $x1 = $width * $quality * .15; $x2 = $x; $y1 = rand($height * $quality * .40, $height * $quality * .65); $y2 = rand($height * $quality * .40, $height * $quality * .65); $lwidth = 0.5 * $quality; for ($i = $lwidth * -1; $i <= $lwidth; $i++) { imageline($im, $x1, $y1 + $i, $x2, $y2 + $i, $imbgcolor); } $imResampled = imagecreatetruecolor($width, $height); imagecopyresampled($imResampled, $im, 0, 0, 0, 0, $width, $height, $width * $quality, $height * $quality); imagedestroy($im); $im = $imResampled; header("Content-type: image/jpeg"); imagejpeg($im, null, 80); imagedestroy($im); } function random($length = 6, $type = 0) { $hash = ''; $chararr = array( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', '0123456789', '23456789ABCDEFGHJKLMNPQRSTUVWXYZ' ); $chars = $chararr[$type]; $max = strlen($chars) - 1; PHP_VERSION < '4.2.0' && mt_srand((double) microtime() * 1000000); for ($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } function gzdecode ($data) { $flags = ord(substr($data, 3, 1)); $headerlen = 10; $extralen = 0; $filenamelen = 0; if ($flags & 4) { $extralen = unpack('v' ,substr($data, 10, 2)); $extralen = $extralen[1]; $headerlen += 2 + $extralen; } if ($flags & 8) // Filename $headerlen = strpos($data, chr(0), $headerlen) + 1; if ($flags & 16) // Comment $headerlen = strpos($data, chr(0), $headerlen) + 1; if ($flags & 2) // CRC at end of file $headerlen += 2; $unpacked = @gzinflate(substr($data, $headerlen)); if ($unpacked === FALSE) $unpacked = $data; return $unpacked; } /* * 生成36位随字符串 */ function makeKey() { return uniqid(uniqid(), true);
mcrypt_encripty加密解密:
# --- 加密 --- # 密钥应该是随机的二进制数据, # 开始使用 scrypt, bcrypt 或 PBKDF2 将一个字符串转换成一个密钥 # 密钥是 16 进制字符串格式 $key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"); # 显示 AES-128, 192, 256 对应的密钥长度: #16,24,32 字节。 $key_size = strlen($key); echo "Key size: " . $key_size . "<br>\n"; $plaintext = "This string was AES-256 / CBC / ZeroBytePadding encrypted."; # 为 CBC 模式创建随机的初始向量 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); # 创建和 AES 兼容的密文(Rijndael 分组大小 = 128) # 仅适用于编码后的输入不是以 00h 结尾的 # (因为默认是使用 0 来补齐数据) $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv); # 将初始向量附加在密文之后,以供解密时使用 $ciphertext = $iv . $ciphertext; # 对密文进行 base64 编码 $ciphertext_base64 = base64_encode($ciphertext); echo $ciphertext_base64 . "<br>\n"; # === 警告 === # 密文并未进行完整性和可信度保护, # 所以可能遭受 Padding Oracle 攻击。 # --- 解密 --- $key_dec = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"); $ciphertext_dec = base64_decode($ciphertext_base64); # 初始向量大小,可以通过 mcrypt_get_iv_size() 来获得 $iv_size_dec = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv_dec = substr($ciphertext_dec, 0, $iv_size_dec); # 获取除初始向量外的密文 $ciphertext_dec = substr($ciphertext_dec, $iv_size_dec); # 可能需要从明文末尾移除 0 $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key_dec, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec); echo $plaintext_dec . "\n";