智一面面试题:php中有几种加密方式?请列举
<h2 style="color: black; text-align: left; margin-bottom: 10px;">php 自带的加密函数</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不可逆的加密函数为:md5()、crypt()</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">md5() 用来计算 MD5 哈稀。语法为:string md5(string str);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">crypt() 将字符串用 UNIX 的标准加密 DES 模块加密。这是单向的加密函数,<span style="color: black;">没</span>法解密。欲比对字符串,将已加密的字符串的头二个字符放在 salt 的参数中,再比对加密后的字符串。语法为:string crypt(string str, string );</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">可逆转的加密为:base64_encode()、urlencode() 相对应的解密函数:base64_decode() 、urldecode()</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">base64_encode() 将字符串以 MIME BASE64 编码。此编码方式<span style="color: black;">能够</span>让中文字<span style="color: black;">或</span><span style="color: black;">照片</span><span style="color: black;">亦</span>能在网络上顺利传输。语法为string base64_encode(string data); 它的解密函数为:string base64_decode(string encoded_data); 将复回原样。</p>urlencode() 将字符串以 URL 编码。例如空格就会变成加号。语法为:string urlencode(string str);
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">它的解密函数为:string urldecode(string str); 将复回原样</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">来<span style="color: black;">瞧瞧</span>以下实现的几种案例</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">①<span style="color: black;">第1</span>种加密解密算法</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><?</span><span style="color: black;">php</span>
<span style="color: black;">function</span> <span style="color: black;">encryptDecrypt</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">,</span> <span style="color: black;">$string</span><span style="color: black;">,</span> <span style="color: black;">$decrypt</span><span style="color: black;">){</span>
<span style="color: black;">if</span><span style="color: black;">(</span><span style="color: black;">$decrypt</span><span style="color: black;">){</span>
<span style="color: black;">$decrypted</span> <span style="color: black;">=</span> <span style="color: black;">rtrim</span><span style="color: black;">(</span><span style="color: black;">mcrypt_decrypt</span><span style="color: black;">(</span><span style="color: black;">MCRYPT_RIJNDAEL_256</span><span style="color: black;">,</span> <span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">),</span> <span style="color: black;">base64_decode</span><span style="color: black;">(</span><span style="color: black;">$string</span><span style="color: black;">),</span> <span style="color: black;">MCRYPT_MODE_CBC</span><span style="color: black;">,</span> <span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">))),</span> <span style="color: black;">"12"</span><span style="color: black;">);</span>
<span style="color: black;">return</span> <span style="color: black;">$decrypted</span><span style="color: black;">;</span>
<span style="color: black;">}</span><span style="color: black;">else</span><span style="color: black;">{</span>
<span style="color: black;">$encrypted</span> <span style="color: black;">=</span> <span style="color: black;">base64_encode</span><span style="color: black;">(</span><span style="color: black;">mcrypt_encrypt</span><span style="color: black;">(</span><span style="color: black;">MCRYPT_RIJNDAEL_256</span><span style="color: black;">,</span> <span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">),</span> <span style="color: black;">$string</span><span style="color: black;">,</span> <span style="color: black;">MCRYPT_MODE_CBC</span><span style="color: black;">,</span> <span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">))));</span>
<span style="color: black;">return</span> <span style="color: black;">$encrypted</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: black;">//加密:"z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk="
</span><span style="color: black;">echo</span> <span style="color: black;">encryptDecrypt</span><span style="color: black;">(</span><span style="color: black;">password</span><span style="color: black;">,</span> <span style="color: black;">Helloweba欢迎您</span><span style="color: black;">,</span><span style="color: black;">0</span><span style="color: black;">);</span>
<span style="color: black;">//解密:"Helloweba欢迎您"
</span><span style="color: black;">echo</span> <span style="color: black;">encryptDecrypt</span><span style="color: black;">(</span><span style="color: black;">password</span><span style="color: black;">,</span> <span style="color: black;">z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=</span><span style="color: black;">,</span><span style="color: black;">1</span><span style="color: black;">);</span>
<span style="color: black;">?></span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">②第二种加密解密算法:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><?php
//加密函数
function lock_url($txt,$key=www.zhuoyuexiazai.com){
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0,64);
$ch = $chars[$nh];
$mdKey = md5($key.$ch);
$mdKey = substr($mdKey,$nh%8, $nh%8+7);
$txt = base64_encode($txt);
$tmp = ;
$i=0;$j=0;$k = 0;
for ($i=0; $i<strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;</div>
楼主的文章非常有意义,提升了我的知识水平。 “BS”(鄙视的缩写) 软文发布平台 http://www.fok120.com/ 祝福你、祝你幸福、早日实现等。
页:
[1]