在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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">urlencode() 将字符串以 URL 编码。例如空格就会变成加号。语法为:string urlencode(string str);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">它的解密函数为:string urldecode(string str); 将复回原样</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">来<span style="color: black;">瞧瞧</span>以下实现的几种案例</p>
<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;"><span style="color: black;"><?</span><span style="color: black;">php</span>
<span style="color: black;">//加密函数
</span><span style="color: black;">function</span> <span style="color: black;">lock_url</span><span style="color: black;">(</span><span style="color: black;">$txt</span><span style="color: black;">,</span><span style="color: black;">$key</span><span style="color: black;">=</span><span style="color: black;">www.zhuoyuexiazai.com</span><span style="color: black;">){</span>
<span style="color: black;">$chars</span> <span style="color: black;">=</span> <span style="color: black;">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"</span><span style="color: black;">;</span>
<span style="color: black;">$nh</span> <span style="color: black;">=</span> <span style="color: black;">rand</span><span style="color: black;">(</span><span style="color: black;">0</span><span style="color: black;">,</span><span style="color: black;">64</span><span style="color: black;">);</span>
<span style="color: black;">$ch</span> <span style="color: black;">=</span> <span style="color: black;">$chars</span><span style="color: black;">[</span><span style="color: black;">$nh</span><span style="color: black;">];</span>
<span style="color: black;">$mdKey</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;">$ch</span><span style="color: black;">);</span>
<span style="color: black;">$mdKey</span> <span style="color: black;">=</span> <span style="color: black;">substr</span><span style="color: black;">(</span><span style="color: black;">$mdKey</span><span style="color: black;">,</span><span style="color: black;">$nh</span><span style="color: black;">%</span><span style="color: black;">8</span><span style="color: black;">,</span> <span style="color: black;">$nh</span><span style="color: black;">%</span><span style="color: black;">8</span><span style="color: black;">+</span><span style="color: black;">7</span><span style="color: black;">);</span>
<span style="color: black;">$txt</span> <span style="color: black;">=</span> <span style="color: black;">base64_encode</span><span style="color: black;">(</span><span style="color: black;">$txt</span><span style="color: black;">);</span>
<span style="color: black;">$tmp</span> <span style="color: black;">=</span> <span style="color: black;">;</span>
<span style="color: black;">$i</span><span style="color: black;">=</span><span style="color: black;">0</span><span style="color: black;">;</span><span style="color: black;">$j</span><span style="color: black;">=</span><span style="color: black;">0</span><span style="color: black;">;</span><span style="color: black;">$k</span> <span style="color: black;">=</span> <span style="color: black;">0</span><span style="color: black;">;</span>
<span style="color: black;">for</span> <span style="color: black;">(</span><span style="color: black;">$i</span><span style="color: black;">=</span><span style="color: black;">0</span><span style="color: black;">;</span> <span style="color: black;">$i</span><span style="color: black;"><</span><span style="color: black;">strlen</span><span style="color: black;">(</span><span style="color: black;">$txt</span><span style="color: black;">);</span> <span style="color: black;">$i</span><span style="color: black;">++</span><span style="color: black;">)</span> <span style="color: black;">{</span>
<span style="color: black;">$k</span> <span style="color: black;">=</span> <span style="color: black;">$k</span> <span style="color: black;">==</span> <span style="color: black;">strlen</span><span style="color: black;">(</span><span style="color: black;">$mdKey</span><span style="color: black;">)</span> <span style="color: black;">?</span> <span style="color: black;">0</span> <span style="color: black;">:</span> <span style="color: black;">$k</span><span style="color: black;">;</span>
<span style="color: black;">$j</span> <span style="color: black;">=</span> <span style="color: black;">(</span><span style="color: black;">$nh</span><span style="color: black;">+</span><span style="color: black;">strpos</span><span style="color: black;">(</span><span style="color: black;">$chars</span><span style="color: black;">,</span><span style="color: black;">$txt</span><span style="color: black;">[</span><span style="color: black;">$i</span><span style="color: black;">])</span><span style="color: black;">+</span><span style="color: black;">ord</span><span style="color: black;">(</span><span style="color: black;">$mdKey</span><span style="color: black;">[</span><span style="color: black;">$k</span><span style="color: black;">++</span><span style="color: black;">]))</span><span style="color: black;">%</span><span style="color: black;">64</span><span style="color: black;">;</span>
<span style="color: black;">$tmp</span> <span style="color: black;">.=</span> <span style="color: black;">$chars</span><span style="color: black;">[</span><span style="color: black;">$j</span><span style="color: black;">];</span>
<span style="color: black;">}</span>
<span style="color: black;">return</span> <span style="color: black;">urlencode</span><span style="color: black;">(</span><span style="color: black;">$ch</span><span style="color: black;">.</span><span style="color: black;">$tmp</span><span style="color: black;">);</span>
<span style="color: black;">}</span>
<span style="color: black;">//解密函数
</span><span style="color: black;">function</span> <span style="color: black;">unlock_url</span><span style="color: black;">(</span><span style="color: black;">$txt</span><span style="color: black;">,</span><span style="color: black;">$key</span><span style="color: black;">=</span><span style="color: black;">www.zhuoyuexiazai.com</span><span style="color: black;">){</span>
<span style="color: black;">$txt</span> <span style="color: black;">=</span> <span style="color: black;">urldecode</span><span style="color: black;">(</span><span style="color: black;">$txt</span><span style="color: black;">);</span>
</div>
感谢楼主的分享!我学到了很多。 感谢楼主的分享!我学到了很多。
页:
[1]