PHP+MySQL应用中运用XOR运算加密算法,这些知识您认识多少?
本文将介绍一个简单易用的加密/解密算法:<span style="color: black;">运用</span>异或(XOR)运算。本算法原理简单,旨在使读者对信息的加密/解密有一个更加直观的印象。<h2 style="color: black; text-align: left; margin-bottom: 10px;">01XOR算法原理</h2><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">从加密的<span style="color: black;">重点</span><span style="color: black;">办法</span>看,换位法过于简单,<span style="color: black;">尤其</span>是<span style="color: black;">针对</span>数据量少的<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;">各样</span>替换法运算的特点看,异或运算最适合用于简易加解密运算,这种<span style="color: black;">办法</span>的原理是:当一个数A和另一个数B进行异或运算会生成另一个数C,<span style="color: black;">倘若</span>再将C和B进行异或运算则C又会还原为A。</p>相<span style="color: black;">针对</span>其他的简易加密算法,XOR算法的优点如下:
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)算法简单,<span style="color: black;">针对</span>高级语言很容易能实现。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)速度快,<span style="color: black;">能够</span>在任何时候、任何<span style="color: black;">地区</span><span style="color: black;">运用</span>。</p>(3)对任何字符都是有效的,不像有些简易加密算法,只对西文字符有效,对中文加密后再解密<span style="color: black;">没</span>法还原为原来的字符。
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">02XOR算法实现</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上一部分介绍了<span style="color: black;">怎样</span><span style="color: black;">运用</span>XOR运算进行加密/解密的原理,本节将<span style="color: black;">运用</span>其加密用户的登录信息。<span style="color: black;">按照</span>上一小节介绍的XOR加密算法的原理,不难写出以下的加密解密函数。<span style="color: black;">首要</span>列出加密算法。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">!--</span><span style="color: black;">encrypy_xor</span><span style="color: black;">:</span><span style="color: black;">简单<span style="color: black;">运用</span>XOR运算的加密函数</span><span style="color: black;">-----------------------</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">?</span><span style="color: black;">php</span>
<span style="color: black;">//加密函数
</span><span style="color: black;">functionmyEncrypt</span><span style="color: black;">(</span><span style="color: black;">$string</span><span style="color: black;">,</span><span style="color: black;">$key</span><span style="color: black;">)</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<STRLEN</span><span style="color: black;">(</span><span style="color: black;">$STRING</span><span style="color: black;">);</span><span style="color: black;">p</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;">{</span>
<span style="color: black;">for</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;">$j<STRLEN</span><span style="color: black;">(</span><span style="color: black;">$KEY</span><span style="color: black;">);</span><span style="color: black;">p</span><span style="color: black;">$j</span><span style="color: black;">++</span><span style="color: black;">)</span><span style="color: black;"><></span>
<span style="color: black;">{</span>
<span style="color: black;">$string</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;">$string</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;">$key</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;">}</span>
<span style="color: black;">13</span><span style="color: black;">return</span><span style="color: black;">$string</span><span style="color: black;">;</span>
<span style="color: black;">14</span><span style="color: black;">}</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第4行定义了加密函数myEncrypt(),输入参数$string为明文,而$key为密钥;输出为<span style="color: black;">运用</span>$key<span style="color: black;">做为</span>密钥并<span style="color: black;">运用</span>XOR加密算法产生的密文.第6~12行的外层for循环对明文字符串的每一个字符进行循环,而内层的for循环(第8~11行)对明文的每一字符循环与密钥的每一位做异或运算。其原理<span style="color: black;">已然</span>在上一小节<span style="color: black;">中间商</span>绍,<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;">一样</span>,与加密函数类似,<span style="color: black;">能够</span>写出下面的解密函数</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">//解密函数
</span><span style="color: black;">functionmyDecrypt</span><span style="color: black;">(</span><span style="color: black;">$string</span><span style="color: black;">,</span><span style="color: black;">$key</span><span style="color: black;">)</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<STRLEN</span><span style="color: black;">(</span><span style="color: black;">$STRING</span><span style="color: black;">);</span><span style="color: black;">p</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;">{</span>
<span style="color: black;">for</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;">$j<STRLEN</span><span style="color: black;">(</span><span style="color: black;">$KEY</span><span style="color: black;">);</span><span style="color: black;">p</span><span style="color: black;">$j</span><span style="color: black;">++</span><span style="color: black;">)</span><span style="color: black;"><></span>
<span style="color: black;">{</span>
<span style="color: black;">$string</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;">$key</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;">$string</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;">return</span><span style="color: black;">$string</span><span style="color: black;">;</span>
<span style="color: black;">}</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;">第4行定义<span style="color: black;">认识</span>密函数myDecrypt(),输入参数$string为密文,而$key为密钥;输出为<span style="color: black;">运用</span>$key<span style="color: black;">做为</span>密钥并<span style="color: black;">运用</span>XOR解密算法产生的明文。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面,<span style="color: black;">经过</span>一个应用示例来进一步说明加密函数的功能</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">//示例
</span><span style="color: black;">$my_password</span><span style="color: black;">=</span><span style="color: black;">"chair"</span><span style="color: black;">;</span>
<span style="color: black;">echo</span><span style="color: black;">"my_password=</span><span style="color: black;">$my_password</span><span style="color: black;">"</span><span style="color: black;">;</span>
<span style="color: black;">$my_key</span><span style="color: black;">=</span><span style="color: black;">"1234567890"</span><span style="color: black;">;</span>
<span style="color: black;">$my_password_en</span><span style="color: black;">=</span><span style="color: black;">myEncrypt</span><span style="color: black;">(</span><span style="color: black;">$my_password</span><span style="color: black;">,</span><span style="color: black;">$my_key</span><span style="color: black;">);</span>
<span style="color: black;">echo</span><span style="color: black;">"my_password_en=</span><span style="color: black;">$my_password_en</span><span style="color: black;">"</span><span style="color: black;">;</span>
<span style="color: black;">$my_password_de</span><span style="color: black;">=</span><span style="color: black;">myDecrypt</span><span style="color: black;">(</span><span style="color: black;">$my_password_en</span><span style="color: black;">,</span><span style="color: black;">$my_key</span><span style="color: black;">);</span>
<span style="color: black;">echo</span><span style="color: black;">"my_password_de=</span><span style="color: black;">$my_password_de</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;">第3行<span style="color: black;">首要</span>定义了一个明文$my_password,<span style="color: black;">而后</span>在第4行定义密钥$my_key。第5、6行分别调用加密函数生成密文并输出;反过来,又在第7、8行将密文解密。</p>
<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;">my_password</span><span style="color: black;">=</span><span style="color: black;">chair</span>
<span style="color: black;">my_password_en</span><span style="color: black;">=</span><span style="color: black;">RYPXC</span>
<span style="color: black;">my_password_de</span><span style="color: black;">=</span><span style="color: black;">chair</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用XOR算法实现身份验证上两部分分别介绍了<span style="color: black;">运用</span>XOR运算进行信息加密/解密的原理和实现,下面,将<span style="color: black;">运用</span>这一<span style="color: black;">办法</span>来对用户的登录<span style="color: black;">暗码</span>进行加密。本例中,为了<span style="color: black;">守护</span>用户的<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;">必须</span>添写用户<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;">自己</span>之外,其他任何人都<span style="color: black;">没</span>法获取其<span style="color: black;">暗码</span>信息,</p>
你的见解真是独到,让我受益良多。 同意、说得对、没错、我也是这么想的等。 期待楼主的下一次分享!” 楼主发的这篇帖子,我觉得非常有道理。
页:
[1]