tw4ld6 发表于 2024-7-11 02:56:08

PHP加密算法分析


    <h2 style="color: black; text-align: left; margin-bottom: 10px;">加密定义</h2>在<span style="color: black;">暗码</span>学中,加密(英语:Encryption)是将明文信息改变为难以读取的密文内容,使之不可读的过程。<span style="color: black;">仅有</span><span style="color: black;">持有</span>解密<span style="color: black;">办法</span>的对象,经由解密过程,<span style="color: black;">才可</span>将密文还原为正常可读的内容。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">加密技术的重点是加密算法,加密算法<span style="color: black;">重点</span>分为三类:</p>对<span style="color: black;">叫作</span>加密非对<span style="color: black;">叫作</span>加密不可逆加密<h2 style="color: black; text-align: left; margin-bottom: 10px;">对<span style="color: black;">叫作</span>加密算法</h2>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">1. 加密过程</h2>将明文分成N个组,<span style="color: black;">而后</span>对各个组进行加密,形成各自的密文,最后把所有的分组密文进行合并,形成<span style="color: black;">最后</span>的密文。<h2 style="color: black; text-align: left; margin-bottom: 10px;">2. 优点</h2>算法公开、计算量小、加密速度快、加密效率高<h2 style="color: black; text-align: left; margin-bottom: 10px;">3.缺点</h2>交易双方都<span style="color: black;">运用</span><span style="color: black;">一样</span>钥匙,安全性得不到<span style="color: black;">保准</span>密钥管理困难,尤其是在分布式网络中<h2 style="color: black; text-align: left; margin-bottom: 10px;">4.常用算法</h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DES、3DES(TripleDES)、AES、RC2、RC4、RC5和Blowfish</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">PHP中对<span style="color: black;">叫作</span>加密算法</h2>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">$mode_list</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_list_modes</span><span style="color: black;">();</span><span style="color: black;">//mcrypt支持的加密模式列表
      </span>
      <span style="color: black;">// print_r($cipher_list);
      </span><span style="color: black;">// print_r($mode_list);
      </span>
      <span style="color: black;">function</span> <span style="color: black;">encrypt</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">,</span><span style="color: black;">$data</span><span style="color: black;">){</span>
      <span style="color: black;">$td</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_module_open</span><span style="color: black;">(</span><span style="color: black;">"des"</span><span style="color: black;">,</span> <span style="color: black;">""</span><span style="color: black;">,</span> <span style="color: black;">"ecb"</span><span style="color: black;">,</span> <span style="color: black;">""</span><span style="color: black;">);</span><span style="color: black;">//<span style="color: black;">运用</span>MCRYPT_DES算法,ecb模式
      </span> <span style="color: black;">$size</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_enc_get_iv_size</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span> <span style="color: black;">//设置初始向量的<span style="color: black;">体积</span>
      </span> <span style="color: black;">$iv</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_create_iv</span><span style="color: black;">(</span><span style="color: black;">$size</span><span style="color: black;">,</span><span style="color: black;">MCRYPT_RAND</span><span style="color: black;">);</span> <span style="color: black;">//创建初始向量</span>
      <span style="color: black;">$key_size</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_enc_get_key_size</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span> <span style="color: black;">//返回所支持的最大的密钥长度(以字节计算)
      </span> <span style="color: black;">$salt</span> <span style="color: black;">=</span> <span style="color: black;">;</span>
      <span style="color: black;">$subkey</span> <span style="color: black;">=</span> <span style="color: black;">substr</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;">.</span><span style="color: black;">$salt</span><span style="color: black;">),</span> <span style="color: black;">0</span><span style="color: black;">,</span><span style="color: black;">$key_size</span><span style="color: black;">);</span><span style="color: black;">//对key<span style="color: black;">繁杂</span>处理,并设置长度
      </span>
      <span style="color: black;">mcrypt_generic_init</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">,</span> <span style="color: black;">$subkey</span><span style="color: black;">,</span> <span style="color: black;">$iv</span><span style="color: black;">);</span>
      <span style="color: black;">$endata</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_generic</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">,</span> <span style="color: black;">$data</span><span style="color: black;">);</span>
      <span style="color: black;">mcrypt_generic_deinit</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span>
      <span style="color: black;">mcrypt_module_close</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span>
      <span style="color: black;">return</span> <span style="color: black;">$endata</span><span style="color: black;">;</span>
      <span style="color: black;">}</span>

      <span style="color: black;">function</span> <span style="color: black;">decrypt</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">,</span><span style="color: black;">$endata</span><span style="color: black;">){</span>
      <span style="color: black;">$td</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_module_open</span><span style="color: black;">(</span><span style="color: black;">"des"</span><span style="color: black;">,</span> <span style="color: black;">""</span><span style="color: black;">,</span> <span style="color: black;">"ecb"</span><span style="color: black;">,</span> <span style="color: black;">""</span><span style="color: black;">);</span><span style="color: black;">//<span style="color: black;">运用</span>MCRYPT_DES算法,ecb模式
      </span> <span style="color: black;">$size</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_enc_get_iv_size</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span> <span style="color: black;">//设置初始向量的<span style="color: black;">体积</span>
      </span> <span style="color: black;">$iv</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_create_iv</span><span style="color: black;">(</span><span style="color: black;">$size</span><span style="color: black;">,</span><span style="color: black;">MCRYPT_RAND</span><span style="color: black;">);</span> <span style="color: black;">//创建初始向量
      </span> <span style="color: black;">$key_size</span> <span style="color: black;">=</span> <span style="color: black;">mcrypt_enc_get_key_size</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span> <span style="color: black;">//返回所支持的最大的密钥长度(以字节计算)
      </span> <span style="color: black;">$salt</span> <span style="color: black;">=</span> <span style="color: black;">;</span>
      <span style="color: black;">$subkey</span> <span style="color: black;">=</span> <span style="color: black;">substr</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;">.</span><span style="color: black;">$salt</span><span style="color: black;">),</span> <span style="color: black;">0</span><span style="color: black;">,</span><span style="color: black;">$key_size</span><span style="color: black;">);</span><span style="color: black;">//对key<span style="color: black;">繁杂</span>处理,并设置长度
      </span> <span style="color: black;">mcrypt_generic_init</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">,</span> <span style="color: black;">$subkey</span><span style="color: black;">,</span> <span style="color: black;">$iv</span><span style="color: black;">);</span>
      <span style="color: black;">$data</span> <span style="color: black;">=</span> <span style="color: black;">rtrim</span><span style="color: black;">(</span><span style="color: black;">mdecrypt_generic</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">,</span> <span style="color: black;">$endata</span><span style="color: black;">))</span><span style="color: black;">.</span><span style="color: black;">\n</span><span style="color: black;">;</span>
      <span style="color: black;">mcrypt_generic_deinit</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span>
      <span style="color: black;">mcrypt_module_close</span><span style="color: black;">(</span><span style="color: black;">$td</span><span style="color: black;">);</span>
      <span style="color: black;">return</span> <span style="color: black;">$data</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;">"www.tencent.com"</span><span style="color: black;">;</span>
      <span style="color: black;">// $data = "返回所支持的最大的密钥长度(<span style="color: black;">触及</span>到发件费啦";
      </span><span style="color: black;">$data</span> <span style="color: black;">=</span> <span style="color: black;">"dadfafdafd,我是一个好<span style="color: black;">孩儿</span>"</span><span style="color: black;">;</span>

      <span style="color: black;">$endata</span> <span style="color: black;">=</span> <span style="color: black;">encrypt</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">,</span><span style="color: black;">$data</span><span style="color: black;">);</span>

      <span style="color: black;">$data1</span> <span style="color: black;">=</span> <span style="color: black;">decrypt</span><span style="color: black;">(</span><span style="color: black;">$key</span><span style="color: black;">,</span><span style="color: black;">$endata</span><span style="color: black;">);</span>

      <span style="color: black;">echo</span> <span style="color: black;">$endata</span><span style="color: black;">;</span> <span style="color: black;">//直接输出,在网页上是乱码,用base64_encode处理,就变成由字符、数组、加号、斜杠等共64种字符注册</span><span style="color: black;">echo</span> <span style="color: black;">base64_encode</span><span style="color: black;">(</span><span style="color: black;">$endata</span><span style="color: black;">);</span>
      <span style="color: black;">echo</span> <span style="color: black;">$data1</span><span style="color: black;">;</span>
    </div>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">非对<span style="color: black;">叫作</span>加密算法</h2>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;">1. <span style="color: black;">运用</span>过程</h2>乙方生成两把密钥(公钥和私钥)甲方获取乙方的公钥,<span style="color: black;">而后</span>用它对信息加密。乙方得




4zhvml8 发表于 2024-11-2 13:14:09

网站建设seio论坛http://www.fok120.com/

4lqedz 发表于 5 天前

回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。
页: [1]
查看完整版本: PHP加密算法分析