基于PHP实现解密或加密Cloudflar邮箱保护


Cloudflare 有一项功能挺不错的,就是将页面上所有的邮箱地址都加密起来,防止机器人抓到然后干坏事。

这项功能要在后台开启 email address obfuscation

之后就可以在页面上加入一个邮箱地址,比如说 abc@abc

查看源代码就能发现类似如下的代码


<a class="__cf_email__" data-cfemail="30515253705152531e535f5d" href="/cdn-cgi/l/email-protection" rel="external nofollow" >
[email protected]
</a>
<script data-cfhash="f9e31" type="text/javascript">
 /* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
</script>

我们可以使用 PHP 的方式将这个邮箱地址解密出来


function deCFEmail($encode){
    $k = hexdec(substr($encode,0,2));
    for($i=2, $m=''; $i < strlen($encode) - 1; $i += 2){
        $m.=chr(hexdec(substr($encode, $i, 2))^$k);
    }
    return $m;
}
echo deCFEmail("30515253705152531e535f5d")."\n";


得到的结果就是上面所说的 abc@abc

但是我们不想用 Cloudflare 的相关服务,就是单纯想用他这种加密技术,所以我们就得将加密的算法也找出来

我们可以利用上面解密的代码反向执行,就可以得到一个加密的算法了


function encodeEmail($email, $key=0) {
    $chars = str_split($email);
    $string = '';
    $key = $key ? $key : rand(10, 99);
    foreach ($chars as $value) {
        $string .= sprintf("%02s", dechex(ord($value)^$key));
    }
    return dechex($key).$string;
}

我们就能利用这个加密算法,将手机号、邮箱地址、身份证等各种敏感的信息都加密起来,防止别人能轻易的抓取到数据

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


上一篇:php 利用socket发送GET,POST请求的实例代码

下一篇:PHP如何使用cURL实现Get和Post请求


Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
热门搜索