Home  >  Article  >  php教程  >  php自定义加密函数、解密的例子

php自定义加密函数、解密的例子

WBOY
WBOYOriginal
2016-05-25 16:45:101173browse

加密就是将一些字符转换为另一个字符串的过程,简单的说就是把大门上锁,并且只有你有开启的钥匙,作为一个程序员都应该有属于自己的加密方式,目前最流行的加密算法就是MD5()加密方式,但是在网站已经有了破解的软件用起来也不是很安全,下面简单的介绍一下自定义的加密函数,代码如下:

<?php
/*加密*/
function addcoder($str) {
    $yuan = &#39;abA!c1dB#ef2@Cg$h%iD_3jkl^E:m}4n.o{&F*p)5q(G-r[sH]6tuIv7w+Jxy8z9K0&#39;;
    $jia = &#39;zAy%0Bx+1C$wDv^Eu2-t3(F{sr&G4q_pH5*on6I)m:l7.Jk]j8K}ih@gf9#ed!cb[a&#39;;
    if (strlen($str) == 0) return false;
    for ($i = 0; $i < strlen($str); $i++) {
        for ($j = 0; $j < strlen($yuan); $j++) {
            if ($str[$i] == $yuan[$j]) {
                $results.= $jia[$j];
                break;
            }
        }
    }
    return $results;
}
/*解密*/
function removecoder($str) {
    $yuan = &#39;abA!c1dB#ef2@Cg$h%iD_3jkl^E:m}4n.o{&F*p)5q(G-r[sH]6tuIv7w+Jxy8z9K0&#39;;
    $jia = &#39;zAy%0Bx+1C$wDv^Eu2-t3(F{sr&G4q_pH5*on6I)m:l7.Jk]j8K}ih@gf9#ed!cb[a&#39;;
    If (strlen($str) == 0) return false;
    for ($i = 0; $i < strlen($str); $i++) {
        for ($j = 0; $j < strlen($jia); $j++) {
            if ($str[$i] == $jia[$j]) {
                $results.= $yuan[$j];
                break;
            }
        }
    }
    return $results;
}
$str = "www.phprm.com";
echo "加密前:" . $str . "<br >";
$str1 = addcoder($str);
echo "加密后:" . $str1 . "<br >";
$str2 = removecoder($str1);
echo "解密后:" . $str2 . "<br >";
?>

例子二,利用md5+base64_encode进行编辑处理,代码如下:

<?php
//
function passport_encrypt($txt, $key) {
    srand((double)microtime() * 1000000);
    $encrypt_key = md5(rand(0, 32000));
    $ctr = 0;
    $tmp = &#39;&#39;;
    for ($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp.= $encrypt_key[$ctr] . ($txt[$i] ^ $encrypt_key[$ctr++]);
    }
    return base64_encode(passport_key($tmp, $key));
}
function passport_decrypt($txt, $key) {
    $txt = passport_key(base64_decode($txt) , $key);
    $tmp = &#39;&#39;;
    for ($i = 0; $i < strlen($txt); $i++) {
        $md5 = $txt[$i];
        $tmp.= $txt[++$i] ^ $md5;
    }
    return $tmp;
}
function passport_key($txt, $encrypt_key) {
    $encrypt_key = md5($encrypt_key);
    $ctr = 0;
    $tmp = &#39;&#39;;
    for ($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp.= $txt[$i] ^ $encrypt_key[$ctr++];
    }
    return $tmp;
}
?>


本文链接:

收藏随意^^请保留教程地址.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn