PHP加密应用场景与原理
在开发过程中,经常需要将敏感数据进行加密处理,即通过特定算法生成加密字符串。这些密文可通过对应的解密算法还原为原始信息,以便程序后续处理。此类技术广泛应用于用户登录验证以及API接口间的数据交互场景。其核心原理是利用加密算法结合密钥,从而达成安全的数据传输与存储目的。
u=3837593897,2803417633&fm=26&gp=0.jpg
接下来直接展示相关代码实现。
第一种方法针对ID进行可逆加密,同样适用于生成邀请码等场景,其解密后的数据结构较为简单直观。
测试示例:lockcode(28) 输出结果为 000X;unlockcode('000X') 输出结果为 28。
//加密函数
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;
}
//解密函数
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;
}
第二种加密函数源自网络搜索,实用性较强,属于可逆加密类型,且支持自定义盐值参数以增强灵活性。
测试示例:encrypt('abcd','1234') 输出结果为 nkiV93IfJ;decrypt('nkiV93IfJ','1234') 输出结果为 abcd。
//加密函数
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
第三种方法与上述类似,同样具备支持盐值参数的特性,便于在不同环境下调整加密策略。
测试示例: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;
}
第四种是作者认为最好用的一个方案,采用了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 '
';
echo authcode('7d49kn9k07uSBZvha8as+/qm4UoLfpy88PFg12glPeDtlzc','key',true);
以上列举的都是作者日常使用且效果良好的加密函数,分享给大家。若需了解其他加密方式或有改进建议,欢迎在评论区留言交流。