分享幾種好用的PHP自定義加密函數(shù)(可逆/不可逆)
項(xiàng)目中有時(shí)我們需要使用PHP將特定的信息進(jìn)行加密,也就是通過加密算法生成一個(gè)加密字符串,這些加密后的字符串可以通過解密算法進(jìn)行解密,便于程序?qū)饷芎蟮男畔⑦M(jìn)行處理。最常見的應(yīng)用在用戶登錄以及一些API數(shù)據(jù)交換的場景。最常見的應(yīng)用在用戶登錄以及一些API數(shù)據(jù)交換的場景。加密解密原理一般都是通過一定的加密解密算法,將密鑰加入到算法中,最終得到加密解密結(jié)果。
u=3837593897,2803417633&fm=26&gp=0.jpg
廢話不多說,直接上代碼。
一、第一種針對于ID的可逆加密函數(shù),也可以用作于邀請碼之類的,解密后的數(shù)據(jù)比較簡單
示例:lockcode(28)=》000X unlockcode('000X')=》28
//加密函數(shù) function lockcode($code) { static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ'; $num = $code; $code = ''; while ( $num > 0) { $mod = $num % 35; $num = ($num - $mod) / 35; $code = $source_string[$mod].$code; } if(empty($code[3])) $code = str_pad($code,4,'0',STR_PAD_LEFT); return $code; } //解密函數(shù) function unlockcode($code) { static $source_string = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ'; if (strrpos($code, '0') !== false) $code = substr($code, strrpos($code, '0')+1); $len = strlen($code); $code = strrev($code); $num = 0; for ($i=0; $i < $len; $i++) { $num += strpos($source_string, $code[$i]) * pow(35, $i); } return $num; }
二、第二種是加密函數(shù)是我在網(wǎng)上搜索來的,很好用,可逆加密,支持鹽值參數(shù)
示例:encrypt('abcd','1234')=》nkiV93IfJ decrypt('nkiV93IfJ','1234')=》abcd
//加密函數(shù) function encrypt($data,$key='CHENI'){ $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; $nh = rand(0,64); $ch = $chars[$nh]; $mdKey = md5($key.$ch); $mdKey = substr($mdKey,$nh%8, $nh%8+7); $data= base64_encode($data); $tmp = ''; $i=0;$j=0;$k = 0; for ($i=0; $i<strlen($data); $i++) { $k = $k == strlen($mdKey) ? 0 : $k; $j = ($nh+strpos($chars,$data[$i])+ord($mdKey[$k++]))%64; $tmp .= $chars[$j]; } return urlencode($ch.$tmp); } //解密函數(shù) function decrypt($data,$key='CHENI'){ $txt = urldecode($data); $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; $ch = $txt[0]; $nh = strpos($chars,$ch); $mdKey = md5($key.$ch); $mdKey = substr($mdKey,$nh%8, $nh%8+7); $txt = substr($txt,1); $tmp = ''; $i=0;$j=0; $k = 0; for ($i=0; $i<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); }
三、第三種跟上面的比較類似,也支持鹽值參數(shù)
示例:encrypt('abcd','1234')=》mZPHxw== decrypt('mZPHxw==','1234')=》abcd
function encrypt($data, $key) { $char=""; $str=""; $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= $key{$x}; $x++; } for ($i = 0; $i < $len; $i++){ $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); } return base64_encode($str); } function decrypt($data, $key) { $key = md5($key); $x = 0; $data = base64_decode($data); $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l){ $x = 0;} $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++){ if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))){ $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); }else{ $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return $str; }
四、這個(gè)是我用過最好用的一個(gè)了,discuz中使用的加密解密算法
//加密算法 function authcode($string,$key='',$operation=false,$expiry=0){ $ckey_length = 4; $key = md5($key ? $key : DEFAULT_KEYS); $keya = md5(substr($key, 0, 16)); $keyb = md5(substr($key, 16, 16)); $keyc = $ckey_length ? ($operation? substr($string, 0, $ckey_length):substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); $string = $operation? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation) { if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { return $keyc.str_replace('=', '', base64_encode($result)); } } echo authcode('123456','key'); echo '<br>'; echo authcode('7d49kn9k07uSBZvha8as+/qm4UoLfpy88PFg12glPeDtlzc','key',true);
以上幾種都是比較好用且是我自己常用的分享給大家,大家如果想要了解其他的加密函數(shù)或者有什么建議可以在底部留言。
相關(guān)文章
php中的四舍五入函數(shù)代碼(floor函數(shù)、ceil函數(shù)、round與intval)
php 中處理浮點(diǎn)數(shù)時(shí)經(jīng)常要需要四舍五入。在php 中有兩個(gè)函數(shù)適用于這種情況:floor函數(shù)、ceil函數(shù)和round函數(shù)2014-07-07php download.php實(shí)現(xiàn)代碼 跳轉(zhuǎn)到下載文件(response.redirect)
一直對php不太熟悉,今天需要類型asp的 response.redirect語句,但一直沒有很好的解決方法。下面是問了朋友才知道的。2009-08-08php數(shù)組函數(shù)序列之a(chǎn)rray_key_exists() - 查找數(shù)組鍵名是否存在
array_key_exists() 函數(shù)判斷某個(gè)數(shù)組中是否存在指定的 key,如果該 key 存在,則返回 true,否則返回 false2011-10-10ob_start(),ob_start(''ob_gzhandler'')使用
ob_start(),ob_start(''ob_gzhandler'')使用...2006-12-12Json_decode 解析json字符串為NULL的解決方法(必看)
下面小編就為大家?guī)硪黄狫son_decode 解析json字符串為NULL的解決方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02PHP實(shí)現(xiàn)向關(guān)聯(lián)數(shù)組指定的Key之前插入元素的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)向關(guān)聯(lián)數(shù)組指定的Key之前插入元素的方法,涉及php針對數(shù)組的遍歷、判斷、獲取、插入等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06PHP實(shí)現(xiàn)的一致性哈希算法完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的一致性哈希算法,以完整實(shí)例形式分析了PHP哈希算法的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11