PHP的Mcrypt加密扩展知识认识
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今天<span style="color: black;">咱们</span>来学习的是 PHP 中的一个过时的扩展 Mcrypt 。在 PHP7 之前,这个扩展是随 PHP 安装包<span style="color: black;">一块</span>内置发布的,<span style="color: black;">然则</span><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>这个扩展的话<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>,<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>会对这套扩展发出过时警告,是<span style="color: black;">由于</span> PHP 更加<span style="color: black;">举荐</span><span style="color: black;">运用</span> OpenSSL 来处理类似的加密能力。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">模块和算法</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Mcrypt <span style="color: black;">重点</span>是<span style="color: black;">运用</span>的 Mcrypt <span style="color: black;">工具</span>来进行加密操作的,<span style="color: black;">因此</span>在 CentOS <span style="color: black;">或</span>其它操作系统中,<span style="color: black;">咱们</span><span style="color: black;">必须</span>安装 libmcrypt-devel 来<span style="color: black;">运用</span>这个扩展。<span style="color: black;">倘若</span> yum 中<span style="color: black;">没</span>法安装的话,直接更新 yum 源<span style="color: black;">就可</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Mcrypt <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> CBC, OFB,CFB 和 ECB 这几种,是一系列的分组、流式加密的模式,有<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><span style="color: black;">自动</span>查阅<span style="color: black;">关联</span>的资料,这里<span style="color: black;">咱们</span>先看一下<span style="color: black;">咱们</span>的环境中所支持的模块和算法。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">$algorithms = @mcrypt_list_algorithms();
print_r($algorithms);
// Array
// (
// => cast-128
// => gost
// => rijndael-128
// => twofish
// => arcfour
// => cast-256
// => loki97
// => rijndael-192
// => saferplus
// => wake
// => blowfish-compat
// => des
// => rijndael-256
// => serpent
// => xtea
// => blowfish
// => enigma
// => rc2
// => tripledes
// )
$modes = @mcrypt_list_modes();
print_r($modes);
// Array
// (
// => cbc
// => cfb
// => ctr
// => ecb
// => ncfb
// => nofb
// => ofb
// => stream
// )</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">mcrypt_list_algorithms() 函数<span style="color: black;">能够</span><span style="color: black;">得到</span>当前环境下所有支持的 Mcrypt 算法。而 mcrypt_list_modes() 则打印出了当前环境下所有可支持的模块。<span style="color: black;">重视</span>在某些版本的 PHP <span style="color: black;">或</span>某些系统中,这些内容会有所<span style="color: black;">区别</span>,在<span style="color: black;">运用</span> Mcrypt <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>有必要在<span style="color: black;">必须</span>运行 Mcrypt 的环境中预先确定好当前环境下所支持的模块和算法。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">加密解密数据</h2>
<div style="color: black; text-align: left; margin-bottom: 10px;">$key = hash(sha256, secret key, true);
$input = json_encode();
$td = @mcrypt_module_open(rijndael-128, , cbc, );
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_URA</div>
页:
[1]