Home >Backend Development >PHP Tutorial >Example of php symmetric encryption algorithm_PHP tutorial

Example of php symmetric encryption algorithm_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:08869browse

Share the implementation code of php symmetric encryption algorithm.

KEY is the previously defined constant

Mcrypt::<span encrypt();
Mcrypt</span>::<span decrypt();

</span><span defined</span>('ROOT') or <span exit</span>('Access Denied'<span );
</span><span class</span><span  Mcrypt{

</span><span public</span> <span static</span> <span function</span> encrypt(<span $code</span><span ){
</span><span return</span> <span base64_encode</span>(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, <span md5</span>(<span KEY</span>), <span $code</span>, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),<span  MCRYPT_RAND)));
}

</span><span public</span> <span static</span> <span function</span> decrypt(<span $code</span><span ){
</span><span return</span> mcrypt_decrypt(MCRYPT_RIJNDAEL_256, <span md5</span>(<span KEY</span>), <span base64_decode</span>(<span $code</span>), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),<span  MCRYPT_RAND));
}
}</span>

xcrypt.php

<span /*</span><span * 
* 常用对称加密算法类 
* 支持密钥:64/128/256 bit(字节长度8/16/32) 
* 支持算法:DES/AES(根据密钥长度自动匹配使用:DES:64bit AES:128/256bit) 
* 支持模式:CBC/ECB/OFB/CFB 
* 密文编码:base64字符串/十六进制字符串/二进制字符串流 
* 填充方式: PKCS5Padding(DES) 
* 
* @author: linvo 
* @version: 1.0.0 
* @date: 2013/1/10 
</span><span */</span> 
<span class</span><span  Xcrypt{

</span><span private</span> <span $mcrypt</span><span ; 
</span><span private</span> <span $key</span><span ; 
</span><span private</span> <span $mode</span><span ; 
</span><span private</span> <span $iv</span><span ; 
</span><span private</span> <span $blocksize</span><span ;

</span><span /*</span><span * 
* 构造函数 
* 
* @param string 密钥 
* @param string 模式 
* @param string 向量("off":不使用 / "auto":自动 / 其他:指定值,长度同密钥) 
</span><span */</span> 
<span public</span> <span function</span> __construct(<span $key</span>, <span $mode</span> = 'cbc', <span $iv</span> = "off"<span ){ 
</span><span switch</span> (<span strlen</span>(<span $key</span><span )){ 
</span><span case</span> 8: 
<span $this</span>->mcrypt =<span  MCRYPT_DES; 
</span><span break</span><span ; 
</span><span case</span> 16: 
<span $this</span>->mcrypt =<span  MCRYPT_RIJNDAEL_128; 
</span><span break</span><span ; 
</span><span case</span> 32: 
<span $this</span>->mcrypt =<span  MCRYPT_RIJNDAEL_256; 
</span><span break</span><span ; 
</span><span default</span>: 
<span die</span>("Key size must be 8/16/32"<span ); 
} www</span>.jbxue.<span com

</span><span $this</span>-><span key</span> = <span $key</span><span ;

</span><span switch</span> (<span strtolower</span>(<span $mode</span><span )){ 
</span><span case</span> 'ofb': 
<span $this</span>->mode =<span  MCRYPT_MODE_OFB; 
</span><span if</span> (<span $iv</span> == 'off') <span die</span>('OFB must give a IV'); <span //</span><span OFB必须有向量 </span>
<span break</span><span ; 
</span><span case</span> 'cfb': 
<span $this</span>->mode =<span  MCRYPT_MODE_CFB; 
</span><span if</span> (<span $iv</span> == 'off') <span die</span>('CFB must give a IV'); <span //</span><span CFB必须有向量 </span>
<span break</span><span ; 
</span><span case</span> 'ecb': 
<span $this</span>->mode =<span  MCRYPT_MODE_ECB; 
</span><span $iv</span> = 'off'; <span //</span><span ECB不需要向量 </span>
<span break</span><span ; 
</span><span case</span> 'cbc': 
<span default</span>: 
<span $this</span>->mode =<span  MCRYPT_MODE_CBC; 
}

</span><span switch</span> (<span strtolower</span>(<span $iv</span><span )){ 
</span><span case</span> "off": 
<span $this</span>->iv = <span null</span><span ; 
</span><span break</span><span ; 
</span><span case</span> "auto": 
<span $source</span> = <span PHP_OS</span>=='WINNT' ? MCRYPT_RAND :<span  MCRYPT_DEV_RANDOM; 
</span><span $this</span>->iv = mcrypt_create_iv(mcrypt_get_block_size(<span $this</span>->mcrypt, <span $this</span>->mode), <span $source</span><span ); 
</span><span break</span><span ; 
</span><span default</span>: 
<span $this</span>->iv = <span $iv</span><span ; 
} 
}

</span><span /*</span><span * 
* 获取向量值 
* @param string 向量值编码(base64/hex/bin) 
* @return string 向量值 
</span><span */</span>  www.jbxue.<span com
</span><span public</span> <span function</span> getIV(<span $code</span> = 'base64'<span ){ 
</span><span switch</span> (<span $code</span><span ){ 
</span><span case</span> 'base64': 
<span $ret</span> = <span base64_encode</span>(<span $this</span>-><span iv); 
</span><span break</span><span ; 
</span><span case</span> 'hex': 
<span $ret</span> = <span bin2hex</span>(<span $this</span>-><span iv); 
</span><span break</span><span ; 
</span><span case</span> 'bin': 
<span default</span>: 
<span $ret</span> = <span $this</span>-><span iv; 
} 
</span><span return</span> <span $ret</span><span ; 
} 

</span><span /*</span><span * 
* 加密 
* @param string 明文 
* @param string 密文编码(base64/hex/bin) 
* @return string 密文 
</span><span */</span> 
<span public</span> <span function</span> encrypt(<span $str</span>, <span $code</span> = 'base64'<span ){ 
</span><span if</span> (<span $this</span>->mcrypt == MCRYPT_DES) <span $str</span> = <span $this</span>->_pkcs5Pad(<span $str</span><span );

</span><span if</span> (<span isset</span>(<span $this</span>-><span iv)) { 
</span><span $result</span> = mcrypt_encrypt(<span $this</span>->mcrypt, <span $this</span>-><span key</span>, <span $str</span>, <span $this</span>->mode, <span $this</span>-><span iv); 
} </span><span else</span><span  { 
@</span><span $result</span> = mcrypt_encrypt(<span $this</span>->mcrypt, <span $this</span>-><span key</span>, <span $str</span>, <span $this</span>-><span mode); 
}

</span><span switch</span> (<span $code</span><span ){ 
</span><span case</span> 'base64': 
<span $ret</span> = <span base64_encode</span>(<span $result</span><span ); 
</span><span break</span><span ; 
</span><span case</span> 'hex': 
<span $ret</span> = <span bin2hex</span>(<span $result</span><span ); 
</span><span break</span><span ; 
</span><span case</span> 'bin': 
<span default</span>: 
<span $ret</span> = <span $result</span><span ; 
}

</span><span return</span> <span $ret</span><span ;

}

</span><span /*</span><span * 
* 解密 
* @param string 密文 
* @param string 密文编码(base64/hex/bin) 
* @return string 明文 
</span><span */</span> 
<span public</span> <span function</span> decrypt(<span $str</span>, <span $code</span> = "base64"<span ){ 
</span><span $ret</span> = <span false</span><span ;

</span><span switch</span> (<span $code</span><span ){ 
</span><span case</span> 'base64': 
<span $str</span> = <span base64_decode</span>(<span $str</span><span ); 
</span><span break</span><span ; 
</span><span case</span> 'hex': 
<span $str</span> = <span $this</span>->_hex2bin(<span $str</span><span ); 
</span><span break</span><span ; 
</span><span case</span> 'bin': 
<span default</span>:<span  
}

</span><span if</span> (<span $str</span> !== <span false</span><span ){ 
</span><span if</span> (<span isset</span>(<span $this</span>-><span iv)) { 
</span><span $ret</span> = mcrypt_decrypt(<span $this</span>->mcrypt, <span $this</span>-><span key</span>, <span $str</span>, <span $this</span>->mode, <span $this</span>-><span iv); 
} </span><span else</span><span  { 
@</span><span $ret</span> = mcrypt_decrypt(<span $this</span>->mcrypt, <span $this</span>-><span key</span>, <span $str</span>, <span $this</span>-><span mode); 
} 
</span><span if</span> (<span $this</span>->mcrypt == MCRYPT_DES) <span $ret</span> = <span $this</span>->_pkcs5Unpad(<span $ret</span><span ); 
</span><span $ret</span> = <span trim</span>(<span $ret</span><span ); 
}

</span><span return</span> <span $ret</span><span ; 
}

</span><span private</span> <span function</span> _pkcs5Pad(<span $text</span><span ){ 
</span><span $this</span>->blocksize = mcrypt_get_block_size(<span $this</span>->mcrypt, <span $this</span>-><span mode); 
</span><span $pad</span> = <span $this</span>->blocksize - (<span strlen</span>(<span $text</span>) % <span $this</span>-><span blocksize); 
</span><span return</span> <span $text</span> . <span str_repeat</span>(<span chr</span>(<span $pad</span>), <span $pad</span><span ); 
}

</span><span private</span> <span function</span> _pkcs5Unpad(<span $text</span><span ){ 
</span><span $pad</span> = <span ord</span>(<span $text</span>{<span strlen</span>(<span $text</span>) - 1<span }); 
</span><span if</span> (<span $pad</span> > <span strlen</span>(<span $text</span>)) <span return</span> <span false</span><span ; 
</span><span if</span> (<span strspn</span>(<span $text</span>, <span chr</span>(<span $pad</span>), <span strlen</span>(<span $text</span>) - <span $pad</span>) != <span $pad</span>) <span return</span> <span false</span><span ; 
</span><span $ret</span> = <span substr</span>(<span $text</span>, 0, -1 * <span $pad</span><span ); 
</span><span return</span> <span $ret</span><span ; 
}

</span><span private</span> <span function</span> _hex2bin(<span $hex</span> = <span false</span><span ){ 
</span><span $ret</span> = <span $hex</span> !== <span false</span> && <span preg_match</span>('/^[0-9a-fA-F]+$/i', <span $hex</span>) ? <span pack</span>("H*", <span $hex</span>) : <span false</span><span ; 
</span><span return</span> <span $ret</span><span ; 
} 
} </span>

<?<span php 
</span><span header</span>('Content-Type:text/html;Charset=utf-8;'<span );

</span><span include</span> "xcrypt.php"<span ;

</span><span echo</span> '<pre class="brush:php;toolbar:false">'<span ; 
</span><span $a</span> = <span isset</span>(<span $_GET</span>['a']) ? <span $_GET</span>['a'] : '测试123'<span ;

</span><span //</span><span 密钥 </span>
<span $key</span> = '12345678123456781234567812345678'; <span //</span><span 256 bit </span>
<span $key</span> = '1234567812345678'; <span //</span><span 128 bit </span>
<span $key</span> = '12345678'; <span //</span><span 64 bit

//设置模式和IV </span>
<span $m</span> = <span new</span> Xcrypt(<span $key</span>, 'cbc', 'auto'<span );

</span><span //</span><span 获取向量值 </span>
<span echo</span> '向量:'<span ; 
</span><span var_dump</span>(<span $m</span>-><span getIV());

</span><span //</span><span 加密 </span>
<span $b</span> = <span $m</span>->encrypt(<span $a</span>, 'base64'<span ); 
</span><span //</span><span 解密 </span>
<span $c</span> = <span $m</span>->decrypt(<span $b</span>, 'base64'<span );

</span><span echo</span> '加密后:'<span ; 
</span><span var_dump</span>(<span $b</span><span ); 
</span><span echo</span> '解密后:'<span ; 
</span><span var_dump</span>(<span $c</span><span ); 
</span><span echo</span> '
';

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/767256.htmlTechArticleShare the implementation code of PHP symmetric encryption algorithm. KEY is the previously defined constant Mcrypt:: :: ('ROOT') or ('Access Denied' encrypt( (mcrypt_encrypt(MCRYPT_RIJNDAEL_256, ( ), , MCRYPT_M...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn