php 7.2 aes 128 ECB 加密

欢迎大家访问我的博客 www.kevink.club


php7.1 开始弃用mcrypt 加密 改为使用 openssl 加密

以下列出 aes ecb 加密 5.* 和 7.2及以上加密方式

php5.*
```

$key = 'Op2TlBNJ3drx71rF';
$string = '{"channel":"djqm","productId":"I19BR9","tranCode":"SPE20200804995150100991","userName":"陈先生","sex":"1011201","mobile1":"18633452430","idcardType":"1010901","idcardNum":"110101199003073773","startTime":"2020-08-04","endTime":"2021-08-04","dataType":"01","cardNum":"IQ990001006"}';

var_dump(aESEncryptRequest($key, $string));
    

function aESEncryptRequest($key, $string){
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $pad = $size - (strlen($string) % $size);
        $input = $string . str_repeat(chr($pad), $pad);
        $td=mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_ECB,'');
        $iv = '';
        mcrypt_generic_init($td, $key, $iv);
        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = bin2hex($data);
        return $data;
    }


```

加密方式使用ECB, 所以偏移量其实被忽略了,在加密过程中并没有涉及到IV.

打印结果
```
9517177c6344a7755f9b9d97dde0999603b37c3c57042583d14d199489bd4778209a171f263cbe62692a9a203829756e2fbad5df07c3af3333c0f6c43242ecb8b56ae8edc4e7a3bf39c82bc232f5562838b4963944612fd32008dc8cf66b753de7fd19413364bc45411c0eb751b0c345aef81bdaecca7fb5a0445c3e4801d815337a0a79b227628a938096a9d24cc0f5fc6c5477240f1bb0b0cc30c3e7d44305f6b79947bf9f483e0b86ec91d7f0a968624368028902232d01f0360b1dc8a7f368ba0dd6564651d0eade270f69795320c6ba4044b7f914265b92cc2aa77d7a45d048975fea6e325711ccc3925e4c6d30c908b2fe24ed3cbf4734c9dd0d16229c49bcff3e9e50fcd656b9f04bbf28006e6489bfba62e22cd1776b1334f0d51d10

````

php7.*

```
 $data = openssl_encrypt($str,"AES-128-ECB",self::YM_ENCODE_KEY,OPENSSL_RAW_DATA);
        $strEncode = bin2hex($data);
        var_dump($strEncode);

```

打印结果
```
9517177c6344a7755f9b9d97dde0999603b37c3c57042583d14d199489bd4778209a171f263cbe62692a9a203829756e176d7b5c679c441c8c3c6e1668c0c8101df5a1e85b8a31f8c48dec71e3ee7d8a38b4963944612fd32008dc8cf66b753de7fd19413364bc45411c0eb751b0c345aef81bdaecca7fb5a0445c3e4801d815337a0a79b227628a938096a9d24cc0f5fc6c5477240f1bb0b0cc30c3e7d44305f6b79947bf9f483e0b86ec91d7f0a968624368028902232d01f0360b1dc8a7f368ba0dd6564651d0eade270f69795320c6ba4044b7f914265b92cc2aa77d7a45d048975fea6e325711ccc3925e4c6d30c908b2fe24ed3cbf4734c9dd0d16229c49bcff3e9e50fcd656b9f04bbf28006e6489bfba62e22cd1776b1334f0d51d10

```
 
加密结果一致。

你可能感兴趣的:(PHP)