Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function strToHex($string)
- {
- $hex="";
- for ($i=0;$i<strlen($string);$i++)
- $hex.=dechex(ord($string[$i]));
- $hex=strtoupper($hex);
- return $hex;
- }
- function hexToStr($hex)
- {
- $string="";
- for ($i=0;$i<strlen($hex)-1;$i+=2)
- $string.=chr(hexdec($hex[$i].$hex[$i+1]));
- return $string;
- }
- function keyED($txt,$encrypt_key){
- $encrypt_key = md5($encrypt_key);
- $ctr=0;
- $tmp = "";
- for ($i=0;$i<strlen($txt);$i++)
- {
- if ($ctr==strlen($encrypt_key)) $ctr=0;
- $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
- $ctr++;
- }
- return $tmp;
- }
- function encrypt($txt,$key){
- srand((double)microtime()*1000000);
- $encrypt_key = md5(rand(0,32000));
- $ctr=0;
- $tmp = "";
- for ($i=0;$i<strlen($txt);$i++)
- {
- if ($ctr==strlen($encrypt_key)) $ctr=0;
- $tmp.= substr($encrypt_key,$ctr,1) .
- (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
- $ctr++;
- }
- $ret=strtohex(base64_encode(keyED($tmp,$key)));
- return substr(md5($ret.$key),0,8).$ret;
- }
- function decrypt($txt,$key){
- //先检验校验码在不在;
- $first_8chars = substr($txt,0,8);
- $last_chars = substr($txt,8);
- $md5_verifier = substr(md5($last_chars.$key),0,8);
- if($md5_verifier !=$first_8chars){
- return "";
- }
- $txt = $last_chars;
- $txt=base64_decode(hextostr($txt));
- $txt = keyED($txt,$key);
- $tmp = "";
- for ($i=0;$i<strlen($txt);$i++)
- {
- $md5 = substr($txt,$i,1);
- $i++;
- $tmp.= (substr($txt,$i,1) ^ $md5);
- }
- return $tmp;
- }
- /**
- //$key="ooomygod这是一个密阴goooo";
- $key="o8342-hfsjfrr3rfs'fgtfjhnbffds'ghfwfrbvdsfsfjhssf'dsfs";
- $string = "需要加密的字符串,如密码等.";
- // 开始加密 encrypt $string, and store it in $enc_text
- $enc_text = encrypt($string,$key);
- // 开始解密 decrypt the encrypted text $enc_text, and store it in $dec_text
- $dec_text = decrypt($enc_text,$key);
- print "原始字符串 : $string <Br>\n";
- print "加密后字串 : $enc_text <Br>\n";
- print "解密后字串 : $dec_text <Br>\n";
- print "to decode:";
- print decrypt("78123be0417539566D515055552B38446F4172575562514A6777616B564F4A5A3851614355374E576E774F42564C42512B565753566555412B464C7857754E5336675332412B3447365153505537525438675456564C634F6F414B415665494439464F47412B454B2B6C48594353633Ds",$key);
- */
- ?>
Add Comment
Please, Sign In to add comment