关于php的aes加密,openssl和mcrypt一些想说的话

  • 内容
  • 评论
  • 相关

1.在php的世界里面mcrypt的生命肯定是已经走到了尽头,从php7.+开始已经不再支持mcrypt函数,需要使用openssl代替。但是这里有个坑,mcrypt对秘钥的长度没有限制,传入多少长度都会参加加密,但是在openssl_encrypt中。key长度只能是16长度,>16长度后,只有前16位参与加密。

2.aes在php7以下用mcrypt实现代码:

<?php
    class AES{
/**
     * This was AES-128 / CBC / PKCS5Padding
     * return base64_encode string
     * @author Terry
     * @param string $plaintext
     * @param string $key
     * @return string
     */
    public static function AesEncrypt($plaintext,$key = null)
    {
        $plaintext = trim($plaintext);
        if ($plaintext == '') return '';
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);


        //PKCS5Padding
        $padding = $size - strlen($plaintext) % $size;
        // 添加Padding
        $plaintext .= str_repeat(chr($padding), $padding);


        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        $key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
  //var_dump($key);die;
        $iv = str_repeat("\0", $size);   //java里面的16个空数组对应的是\0.
  //var_dump($iv);die;
  //echo $key;die;
        /* Intialize encryption */
        mcrypt_generic_init($module, $key, $iv);


        /* Encrypt data */
        $encrypted = mcrypt_generic($module, $plaintext);


        /* Terminate encryption handler */
        mcrypt_generic_deinit($module);
        mcrypt_module_close($module);
        return base64_encode($encrypted);
    }

    /**
     * Returns the length of the given string.
     * If available uses the multibyte string function mb_strlen.
     * @param string $string the string being measured for length
     * @return integer the length of the string
     */
    private static function strlen($string)
    {
        return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
    }


    /**
     * Returns the portion of string specified by the start and length parameters.
     * If available uses the multibyte string function mb_substr
     * @param string $string the input string. Must be one character or longer.
     * @param integer $start the starting position
     * @param integer $length the desired portion length
     * @return string the extracted part of string, or FALSE on failure or an empty string.
     */
    private static function substr($string,$start,$length)
    {
        return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
    }
    /**
     * This was AES-128 / CBC / PKCS5Padding
     * @author Terry
     * @param string $encrypted     base64_encode encrypted string
     * @param string $key
     * @throws CException
     * @return string
     */
    public static function AesDecrypt($encrypted, $key = null)
    {
        if ($encrypted == '') return '';
        $ciphertext_dec = base64_decode($encrypted);
        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        $key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
       
        $iv = str_repeat("\0", 16);    //解密的初始化向量要和加密时一样。
        /* Initialize encryption module for decryption */
        mcrypt_generic_init($module, $key, $iv);


        /* Decrypt encrypted string */
        $decrypted = mdecrypt_generic($module, $ciphertext_dec);


        /* Terminate decryption handle and close module */
        mcrypt_generic_deinit($module);
        mcrypt_module_close($module);
        $a = rtrim($decrypted,"\0");


        return rtrim($decrypted,"\0");
    }
}


 $plain="111111";
 echo  "加密后:".$miwen=AES::AesEncrypt($plain,'mkeymkeymkeymkey');
 echo "<br/>";
 echo "解密后:".AES::AesDecrypt($miwen,'mkeymkeymkeymkey');

?>

运行结果:

加密后:CEBqq9fGmsu4G87+MA2BWg==
解密后:111111

3.php7.+以上openssl实现aes加密代码实现:

<?php

class Aes
 {
    public $key = '';
    public $iv = '';
    public function __construct( $config )
    {
        foreach ( $config as $k => $v ) {
            $this->$k = $v;
        }
    }
    //加密
    public function aesEn( $data ) {
        return  base64_encode( openssl_encrypt( $data, $this->method, $this->key,OPENSSL_RAW_DATA, $this->iv ) );

    }
    //解密

    public function aesDe( $data ) {
        return openssl_decrypt( base64_decode( $data ),  $this->method, $this->key,OPENSSL_RAW_DATA, $this->iv );
    }
}


$config = [
    'key' => 'mkeymkeymkeymkey', //加密key
    'iv' =>  str_repeat("\0", 16),//str_repeat("\0", 16)保证偏移量为16位,这里是16位空字符串,也可以和key一样16字符串,还可以是变化的,比如md5成16位原文,substr(md5("haha"),8,16),变化的需要保证同一个字符串加解密的iv保持一致。
    'method' => 'AES-128-CBC' //加密方式  # AES-256-CBC等
];

 

//openssl_encrypt的第四个参数为1或者OPENSSL_RAW_DATA时填充方式为pks5padding或者pks7padding的结果一样,其他待验证
$obj = new Aes( $config );
//加密数据
$miwen = $obj->aesEn( '111111' );
echo "加密后:".$miwen;
echo "<br/>";
echo "解密后:".$obj->aesDe( $miwen );

?>

运行结果:

加密后:CEBqq9fGmsu4G87+MA2BWg==
解密后:111111

4.结果发现低版本的php的mcrypt和高版本php的openssl,在秘钥都是16位的情况下,加密和解密的结果是一致的。

5.给mcrypt和openssl的秘钥都增加一位x看下结果:

mcrpyt结果:

加密后:6TyYm0gpdVOrEFDOXSy+bA==
解密后:111111

openssl结果:

加密后:CEBqq9fGmsu4G87+MA2BWg==
解密后:111111

 6.总结密钥长度增加一个x,变成17位后,mcrpyt加密后的值发生了变化,但是可以通过相同的密钥解密,openssl加密的值没有发生变化,说明增加到17位的x,根本没有参与加密。

7.如果php程序由低版本mcrypt的aes加密升级到高版本的openssl的aes加密,在密钥不是16位的情况下,就会出现悲剧的情况。还有就是和一些其他语言接口对接,如果别的语言key不是16位,那么他加密后的串,用openssl无法解密。

本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:关于php的aes加密,openssl和mcrypt一些想说的话 - https://wlphp.com/?post=271

发表评论

电子邮件地址不会被公开。 必填项已用*标注