5ep9lzv 发表于 2024-7-11 01:07:25

php几种常用的加密解密算法


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文给<span style="color: black;">大众</span>介绍php的三种常用的加密解密算法,有<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;">php 自带的加密函数:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不可逆的加密函数为:md5()、sha1()、crypt()</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">md5() 用来计算 MD5 哈稀</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">string</span><span style="color: black;">$str</span><span style="color: black;">[,</span><span style="color: black;">bool</span><span style="color: black;">$raw_output</span><span style="color: black;">=</span><span style="color: black;">FALSE</span><span style="color: black;">]</span> <span style="color: black;">)</span> <span style="color: black;">:</span><span style="color: black;">string</span>
    </div>
    <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;"> * md5 有两个参数:
      </span><span style="color: black;"> * <span style="color: black;">第1</span>个参数:加密的字符串
      </span><span style="color: black;"> * 第二个参数:被设置为 TRUE则返回16字节的原始二进制格式,默认 false 以 32 字符十六进制数字形式返回散列值
      </span><span style="color: black;"> */</span>
      <span style="color: black;">$str</span> <span style="color: black;">=</span> <span style="color: black;">123456789</span><span style="color: black;">;</span>
      <span style="color: black;">echo</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;">$str</span><span style="color: black;">)</span><span style="color: black;">.</span><span style="color: black;">"</span><span style="color: black;">\r\n</span><span style="color: black;">"</span><span style="color: black;">;</span> <span style="color: black;">//25f9e794323b453885f5181f1b624d0b
      </span><span style="color: black;">echo</span> <span style="color: black;">第二个参数为TRUE MD5加密的字符串为:</span><span style="color: black;">.</span><span style="color: black;">md5</span><span style="color: black;">(</span><span style="color: black;">$str</span><span style="color: black;">,</span><span style="color: black;">true</span><span style="color: black;">)</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;">;</span> <span style="color: black;">//%��2;E8��bM
      </span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sha1()—计算字符串的 sha1 散列值</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">sha1</span><span style="color: black;">(</span><span style="color: black;">string</span><span style="color: black;">$str</span><span style="color: black;">[,</span><span style="color: black;">bool</span><span style="color: black;">$raw_output</span><span style="color: black;">=</span> <span style="color: black;">false</span><span style="color: black;">]</span> <span style="color: black;">)</span> <span style="color: black;">:</span><span style="color: black;">string</span>
    </div>
    <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;"> * sha1 有两个参数:
      </span><span style="color: black;"> * <span style="color: black;">第1</span>个参数:加密的字符串
      </span><span style="color: black;"> * 第二个参数:被设置为 TRUE则返回20字节的原始二进制格式,默认 false 以 40 字符十六进制数字形式返回散列值
      </span><span style="color: black;"> */</span>
      <span style="color: black;">$str</span> <span style="color: black;">=</span> <span style="color: black;">123456789</span><span style="color: black;">;</span>
      <span style="color: black;">echo</span> <span style="color: black;">默认sha1加密的字符串为:</span><span style="color: black;">.</span><span style="color: black;">sha1</span><span style="color: black;">(</span><span style="color: black;">$str</span><span style="color: black;">)</span><span style="color: black;">.</span><span style="color: black;">"</span><span style="color: black;">\r\n</span><span style="color: black;">"</span><span style="color: black;">;</span> <span style="color: black;">//d0be2dc421be4fcd0172e5afceea3970e2f3d940
      </span><span style="color: black;">echo</span> <span style="color: black;">第二个参数为TRUE sha1加密的字符串为:</span><span style="color: black;">.</span><span style="color: black;">sha1</span><span style="color: black;">(</span><span style="color: black;">$str</span><span style="color: black;">,</span><span style="color: black;">true</span><span style="color: black;">)</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;">;</span> <span style="color: black;">//%��2;E8��bM
      </span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">crypt() 将字符串用 UNIX 的标准加密 DES 模块加密。这是单向的加密函数,<span style="color: black;">没</span>法解密。欲比</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对字符串,将已加密的字符串的头二个字符放在 salt 的参数中,再比对加密后的字符串。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">crypt</span><span style="color: black;">(</span><span style="color: black;">string</span><span style="color: black;">$str</span><span style="color: black;">[,</span><span style="color: black;">string</span><span style="color: black;">$salt</span><span style="color: black;">]</span> <span style="color: black;">)</span> <span style="color: black;">:</span><span style="color: black;">string</span>
    </div>
    <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;"> * crypt 有两个参数:
      </span><span style="color: black;"> * <span style="color: black;">第1</span>个参数:加密字符串
      </span><span style="color: black;">* 第二个参数:盐值字符串</span><span style="color: black;"> */</span>
      <span style="color: black;">$str</span> <span style="color: black;">=</span> <span style="color: black;">123456789</span><span style="color: black;">;</span>
      <span style="color: black;">// <span style="color: black;">运用</span>自动盐值
      </span><span style="color: black;">echo</span> <span style="color: black;">自动 盐值: </span> <span style="color: black;">.</span> <span style="color: black;">crypt</span><span style="color: black;">(</span><span style="color: black;">$str</span><span style="color: black;">)</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;">;</span> <span style="color: black;">// $1$yvnZHBo0$sBZBd0vzZYnxbtQftQulZ/
      </span><span style="color: black;">//以<span style="color: black;">区别</span>散列类型<span style="color: black;">运用</span> crypt()
      </span><span style="color: black;">/**
      </span><span style="color: black;">* CRYPT_STD_DES - 基于标准 DES 算法的散列<span style="color: black;">运用</span> "./0-9A-Za-z" 字符中的两个字符<span style="color: black;">做为</span>盐值。在盐值中<span style="color: black;">运用</span><span style="color: black;">违法</span>的字符将<span style="color: black;">引起</span> crypt() 失败。</span><span style="color: black;">* CRYPT_EXT_DES - 扩展的基于 DES 算法的散列。其盐值为 9 个字符的字符串,由 1 个下划线后面跟着 4 字节循环次数和 4 字节盐值<span style="color: black;">构成</span>。它们被编码成可打印字符,<span style="color: black;">每一个</span>字符 6 位,有效位最少的优先。0 到 63 被编码为 "./0-9A-Za-z"。在盐值中<span style="color: black;">运用</span><span style="color: black;">违法</span>的字符将<span style="color: black;">引起</span> crypt() 失败。</span><span style="color: black;"> * CRYPT_MD5 - MD5 散列<span style="color: black;">运用</span>一个以 $1$ <span style="color: black;">起始</span>的 12 字符的字符串盐值。
      </span><span style="color: black;">* CRYPT_BLOWFISH - Blowfish 算法<span style="color: black;">运用</span>如下盐值:“$2a$”,一个两位 cost 参数,“$” 以及 64 位由 “./0-9A-Za-z” 中的字符组合而成的字符串。在盐值中<span style="color: black;">运用</span>此范围之外的字符将<span style="color: black;">引起</span> crypt() 返回一个空字符串。两位 cost 参数是循环次数以 2 为底的对数,它的范围是 04-31,超出这个范围将<span style="color: black;">引起</span> crypt() 失败。 PHP 5.3.7 之前只支持 “$2a$” <span style="color: black;">做为</span>盐值的前缀,PHP 5.3.7 <span style="color: black;">起始</span>引入了新的前缀来修正一个在Blowfish实现上的安全<span style="color: black;">危害</span>。<span style="color: black;">能够</span>参考» this document来<span style="color: black;">认识</span>关于这个修复的<span style="color: black;">更加多</span>信息。总而言之,<span style="color: black;">研发</span>者<span style="color: black;">倘若</span>仅针对 PHP</span>
    </div>




4zhvml8 发表于 2024-10-30 02:40:49

我深感你的理解与共鸣,愿对话长流。

nykek5i 发表于 1 小时前

楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/
页: [1]
查看完整版本: php几种常用的加密解密算法