1.第一种
<?php
function encryptDecrypt(key,string, $decrypt){
if($decrypt){
decrypted=rtrim(mcryptdecrypt(MCRYPTRIJNDAEL256,md5(key), base64_decode(string),MCRYPTMODECBC,md5(md5(key))), "12");
return $decrypted;
}else{
encrypted=base64encode(mcryptencrypt(MCRYPTRIJNDAEL256,md5(key), string,MCRYPTMODECBC,md5(md5(key))));
return $encrypted;
}//代码效果参考:http://www.ezhiqi.com/zx/art_3160.html
}
//加密:"z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk="
echo encryptDecrypt('password', 'Helloweba欢迎您',0);
//解密:"Helloweba欢迎您"
echo encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);
?>
2.第二种
<?php
//加密函数
function lock_url(txt,key='liiu'){
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0,64);
ch=chars【$nh】;
mdKey=md5(key.$ch);
mdKey=substr(mdKey,nhnh%8+7);
txt=base64encode(txt);
$tmp = '';
i=0;j=0;$k = 0;
for (i=0;i[span style="color: rgba(0, 128, 128, 1)">strlen(txt);i++) {
k=k == strlen(mdKey)?0:k;
j=(nh+strpos(chars,txt【i】)+ord(mdKey【$k++】))%64;
tmp.=chars【$j】;
}
return urlencode(ch.tmp);
}
//解密函数
function unlock_url(txt,key='liiu'){
txt=urldecode(txt);
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
ch=txt【0】;
nh=strpos(chars,$ch);
mdKey=md5(key.$ch);
mdKey=substr(mdKey,nhnh%8+7);
txt=substr(txt,1);
$tmp = '';
i=0;j=0; $k = 0;
for (i=0;i[span style="color: rgba(0, 128, 128, 1)">strlen(txt);i++) {
k=k == strlen(mdKey)?0:k;
j=strpos(chars,txt【i】)-nh−ord(mdKey【$k++】);
while (j<0)j+=64;
tmp.=chars【$j】;
}
return base64_decode($tmp);
}
?>
3.第三种
<?php
//改进后的算法
//加密函数
function lock_url(txt,key='str'){
txt=txt.$key;
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0,64);
ch=chars【$nh】;
mdKey=md5(key.$ch);
mdKey=substr(mdKey,nhnh%8+7);
txt=base64encode(txt);
$tmp = '';
i=0;j=0;$k = 0;
for (i=0;i[span style="color: rgba(0, 128, 128, 1)">strlen(txt);i++) {
k=k == strlen(mdKey)?0:k;
j=(nh+strpos(chars,txt【i】)+ord(mdKey【$k++】))%64;
tmp.=chars【$j】;
}
return urlencode(base64_encode(ch.tmp));
}
//解密函数
function unlock_url(txt,key='str'){
txt=base64decode(urldecode(txt));
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
ch=txt【0】;
nh=strpos(chars,$ch);
mdKey=md5(key.$ch);
mdKey=substr(mdKey,nhnh%8+7);
txt=substr(txt,1);
$tmp = '';
i=0;j=0; $k = 0;
for (i=0;i[span style="color: rgba(0, 128, 128, 1)">strlen(txt);i++) {
k=k == strlen(mdKey)?0:k;
j=strpos(chars,txt【i】)-nh−ord(mdKey【$k++】);
while (j<0)j+=64;
tmp.=chars【$j】;
}
return trim(base64_decode(tmp),key);
}
?>
4.第四种
<?php
function passport_encrypt(txt,key = 'liiu') {
srand((double)microtime() * 1000000);
$encrypt_key = md5(rand(0, 32000));
$ctr = 0;
$tmp = '';
for(i=0;i < strlen(txt);i++) {
ctr=ctr == strlen(encryptkey)?0:ctr;
tmp.=encrypt_key【ctr】.(txt【i】 ^encrypt_key【$ctr++】);
}
return urlencode(base64_encode(passport_key(tmp,key)));
}//代码效果参考:http://www.ezhiqi.com/bx/art_4413.html
function passport_decrypt(txt,key = 'liiu') {
txt=passportkey(base64decode(urldecode(txt)), $key);
$tmp = '';
for(i=0;i < strlen(txt);i++) {
md5=txt【$i】;
tmp.=txt【++i】 ^md5;
}
return $tmp;
}
function passport_key(txt,encrypt_key) {
encryptkey=md5(encrypt_key);
$ctr = 0;
$tmp = '';
for(i=0;i < strlen(txt);i++) {
ctr=ctr == strlen(encryptkey)?0:ctr;
tmp.=txt【i】 ^encrypt_key【$ctr++】;
}
return $tmp;
}
$txt = "1";
$key = "testkey";
encrypt=passportencrypt(txt,$key);
decrypt=passportdecrypt(encrypt,$key);
echo $encrypt."
";
echo $decrypt."
";
?>
5.第五种
<?php
//非常给力的authcode加密函数,Discuz!经典代码(带详解)
//函数authcode(string,operation, key,expiry)中的string:字符串,明文或密文;operation:DECODE表示解密,其它表示加密;key:密匙;expiry:密文有效期。
function authcode(string,operation = 'DECODE', key=″,expiry = 0) {
// 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
$ckey_length = 4;
// 密匙
key=md5(key ? key:GLOBALS【'discuz_auth_key'】);
// 密匙a会参与加解密
keya=md5(substr(key, 0, 16));
// 密匙b会用来做数据完整性验证
keyb=md5(substr(key, 16, 16));
// 密匙c用于变化生成的密文
keyc=ckey_length ? (operation==′DECODE′?substr(string, 0, ckeylength):substr(md5(microtime()),−ckey_length)) : '';
// 参与运算的密匙
cryptkey=keya.md5(keya.keyc<span style="c