使用AES加密算法进行数据加密和解密

原创文章 作者:月光光 2017年02月07日 20:32helloweba.com 标签:PHP 

AES加密是一种高级加密标准,AES加密采用对称分组密码体制,AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个(如果数据块及密钥长度不足时,会补齐)。

项目中用到了AES加密和解密数据,主要用在网络请求过程中对上传的参数进行加密,对从后台服务器获取的数据进行解密。

我们可以使用AES加密算法将数据加密起来,然后发送给后端,后端再将接收的数据用约定的密钥将数据还原,即解密,Aes算法加密后的数据在传输过程中不易被破解。

在PHP中,我们需要先确保php的环境中安装好了Mcrypt扩展。PHP的mcrypt库提供了对多种块算法的支持,支持 CBC,OFB,CFB 和 ECB 密码模式,mcrypt库提供了丰富的函数使用,有兴趣的同学可以查阅PHP手册。

我已经将aes加解密封装成类,方便调用,在DEMO中可以看到调用效果。

<?php
class Aes
{
    private $secrect_key;

    public function __construct($secrect_key)
    {
        $this->secrect_key = $secrect_key;
    }

    // 加密
    public function encrypt($str)
    {
        $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
        $iv = $this->createIv($cipher);
        if (mcrypt_generic_init($cipher, $this->pad2Length($this->secrect_key, 16), $iv) != -1){
            // PHP pads with NULL bytes if $content is not a multiple of the block size..    
            $cipherText = mcrypt_generic($cipher, $this->pad2Length($str, 16));
            mcrypt_generic_deinit($cipher);
            mcrypt_module_close($cipher);

            return bin2hex($cipherText);
        }
    }

    public function decrypt($str)
    {
        $padkey = $this->pad2Length($this->secrect_key, 16);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
        $iv = $this->createIv($td);
        if (mcrypt_generic_init($td, $padkey, $iv) != -1){    
            $p_t = mdecrypt_generic($td, $this->hexToStr($str));
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
               
            return $this->trimEnd($p_t);  
        }    
    }

    // IV自动生成
    private function createIv($td)
    {
        $iv_size = mcrypt_enc_get_iv_size($td); 
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        return $iv;
    }

    // 将$text补足$padlen倍数的长度    
    private function pad2Length($text, $padlen)
    {
        $len = strlen($text)%$padlen;
        $res = $text;
        $span = $padlen-$len;
        for ($i=0; $i<$span; $i++) {
            $res .= chr($span);
        }
        return $res;
    }

    // 将解密后多余的长度去掉(因为在加密的时候 补充长度满足block_size的长度)    
    private function trimEnd($text){    
        $len = strlen($text);
        $c = $text[$len-1];
        if(ord($c) < $len){
            for($i=$len-ord($c); $i<$len; $i++) {
                if($text[$i] != $c){
                    return $text;
                }
            }
            return substr($text, 0, $len-ord($c));
        }
        return $text;
    }

    //16进制的转为2进制字符串    
    private function hexToStr($hex){
        $bin="";
        for($i=0; $i<strlen($hex)-1; $i+=2) {
            $bin.=chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $bin;
    }
}

调用Aes类进行加密和解密方法如下:

<?php
$key = 'MYgGnQE2jDFADSFFDSEWsdD2'; //密钥
$str = 'abc'; //要加密的字符串
$aes = new Aes($key);
//加密
echo $aes->encrypt($str);
//解密
echo $aes->decrypt($str);
声明:本文为原创文章,helloweba.net和作者拥有版权,如需转载,请注明来源于helloweba.net并保留原文链接:https://www.helloweba.net/php/407.html

0条评论