代码由来
这个代码是我一次需要加密字符串的时候做的,想的是加密后还能解密回来,一直在找对称加密,但都不太理想,于是就想着变异base,奈何自己技术菜,只能随便变异一下,达到的效果是每次加密的效果都可能不一样,用以迷惑别人。
代码
代码找的是PHP实现的base64,变异后结果如下:
<?php
function Encryption_Core($num){//明文转加密normalToBase64Char
if ($num >= 0 && $num <= 25) {
return chr(ord('A') + $num);
} else if ($num >= 26 && $num <= 51) {
return chr(ord('a') + ($num - 26));
} else if ($num >= 52 && $num <= 61) {
return chr(ord('0') + ($num - 52));
} else if ($num == 62) {
return '+';
} else {
return '/';
}
}
function Encryption($content){//加密处理函数
$len = strlen($content);
$loop = intval($len / 3);//完整组合
$rest = $len % 3;//剩余字节数,需要补齐
$ret = "";
//首先计算完整组合
for ($i = 0; $i < $loop; $i++) {
$base_offset = 3 * $i;
$int_24 = (ord($content[$base_offset]) << 16)
| (ord($content[$base_offset + 1]) << 8)
| (ord($content[$base_offset + 2]) << 0);
$ret .= Encryption_Core($int_24 >> 18);
$ret .= Encryption_Core(($int_24 >> 12) & 0x3f);
$ret .= Encryption_Core(($int_24 >> 6) & 0x3f);
$ret .= Encryption_Core($int_24 & 0x3f);
}
//需要补齐的情况
if ($rest == 0) {
$res = $ret;
} else if ($rest == 1) {
//剩余1个字节,此时需要补齐4位
$int_12 = ord($content[$loop * 3]) << 4;
$ret .= Encryption_Core($int_12 >> 6);
$ret .= Encryption_Core($int_12 & 0x3f);
$ret .= "==";
$res = $ret;
} else {
//剩余2个字节,需要补齐2位
$int_18 = ((ord($content[$loop * 3]) << 8) | ord($content[$loop * 3 + 1])) << 2;
$ret .= Encryption_Core($int_18 >> 12);
$ret .= Encryption_Core(($int_18 >> 6) & 0x3f);
$ret .= Encryption_Core($int_18 & 0x3f);
$ret .= "=";
$res = $ret;
}
$rand = rand(1,9);
switch ($rand) {
case '1':
$encode = "sdiasjdsad";
break;
case '2':
$encode = "sdeegff";
break;
case '3':
$encode = "gfjfc";
break;
case '4':
$encode = "hodjfj";
break;
case '5':
$encode = "dzsdsad";
break;
case '6':
$encode = "dfgfheu";
break;
case '7':
$encode = "feusdith";
break;
case '8':
$encode = "dsjdeix";
break;
case '9':
$encode = "ogfkrsa";
break;
default:
$encode = "idsaw";
break;
}
$str1 = substr($res , 0, 1 );//截取第一位
$str2 = substr($res , 1, 1 );//第二个字符
$str3 = substr($res,2);//删除二个字符
$newstr = $str1.$rand.$str2.$encode.$str3;//字符串base64后,第一个字符+随机数+第二个字符+干扰代码+删除第一、第二个的剩余字符
return $newstr;
}
function Decryption_Core($num){//加密转明文base64CharToInt 解密
if ($num >= 65 && $num <= 90) {
return ($num - 65);
} else if ($num >= 97 && $num <= 122) {
return ($num - 97)+26;
} else if ($num >= 48 && $num <= 57) {
return ($num - 48)+52;
} else if ($num == 43) {
return 62;
} else {
return 63;
}
}
function Decryption($content){
$str1 = substr($content , 0, 1 );//截取第一位
$str2 = substr($content , 1, 1 );//截取第二位
$str3 = substr($content,3);//删除三个字符
switch ($str2) {
case '1':
$encode = "sdiasjdsad";
break;
case '2':
$encode = "sdeegff";
break;
case '3':
$encode = "gfjfc";
break;
case '4':
$encode = "hodjfj";
break;
case '5':
$encode = "dzsdsad";
break;
case '6':
$encode = "dfgfheu";
break;
case '7':
$encode = "feusdith";
break;
case '8':
$encode = "dsjdeix";
break;
case '9':
$encode = "ogfkrsa";
break;
default:
$encode = "idsaw";
break;
}
$str4 = substr($content , 2, 1 );//截取第三位
$encode_length = strlen($encode);//干扰代码的长度
$str5 = substr($str3,$encode_length);//剔除干扰代码后的代码
$content = $str1.$str4.$str5;
$len = strlen($content);
if ($content[$len - 1] == '=' && $content[$len - 2] == '=') {
//说明加密的时候,剩余1个字节,补齐了4位,也就是左移了4位,所以除了最后包含的2个字符,前面的所有字符可以4个字符一组
$last_chars = substr($content, -4);
$full_chars = substr($content, 0, $len - 4);
$type = 1;
} else if ($content[$len - 1] == '=') {
//说明加密的时候,剩余2个字节,补齐了2位,也就是左移了2位,所以除了最后包含的3个字符,前面的所有字符可以4个字符一组
$last_chars = substr($content, -4);
$full_chars = substr($content, 0, $len - 4);
$type = 2;
} else {
$type = 3;
$full_chars = $content;
}
//首先处理完整的部分
$loop = strlen($full_chars) / 4;
$ret = "";
for ($i = 0; $i < $loop; $i++) {
$base_offset = 4 * $i;
$int_24 = (Decryption_Core(ord($full_chars[$base_offset])) << 18)
| (Decryption_Core(ord($full_chars[$base_offset + 1])) << 12)
| (Decryption_Core(ord($full_chars[$base_offset + 2])) << 6)
| (Decryption_Core(ord($full_chars[$base_offset + 3])) << 0);
$ret .= chr($int_24 >> 16);
$ret .= chr(($int_24 >> 8) & 0xff);
$ret .= chr($int_24 & 0xff);
}
//紧接着处理补齐的部分
if ($type == 1) {
$l_char = chr(((Decryption_Core(ord($last_chars[0])) << 6)
| (Decryption_Core(ord($last_chars[1])))) >> 4);
$ret .= $l_char;
} else if ($type == 2) {
$l_two_chars = ((Decryption_Core(ord($last_chars[0])) << 12)
| (Decryption_Core(ord($last_chars[1])) << 6)
| (Decryption_Core(ord($last_chars[2])) << 0)) >> 2;
$ret .= chr($l_two_chars >> 8);
$ret .= chr($l_two_chars & 0xff);
}
return $ret;
}
结语
大家可以自己看看,自己改改,我改的也非常简单。
© 版权声明
THE END
暂无评论内容