Prpcrypt.php
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace app\api\helpers\wxencrypt;
use yii\base\Exception;
use function mcrypt_module_open;
use function mcrypt_generic_init;
use function mdecrypt_generic;
use function mcrypt_generic_deinit;
use function mcrypt_module_close;
/**
* Prpcrypt class
*
*
*/
class Prpcrypt
{
/**
* 对密文进行解密
* @param string $aesKey 解密的秘钥
* @param string $aesCipher 需要解密的密文
* @param string $aesIV 解密的初始向量
* @return string 解密得到的明文
*/
public static function decrypt($aesKey, $aesCipher, $aesIV )
{
try {
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($module, $aesKey, $aesIV);
//解密
$decrypted = mdecrypt_generic($module, $aesCipher);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
} catch (Exception $e) {
return array(ErrorCode::$IllegalBuffer, null);
}
try {
//去除补位字符
$result = PKCS7Encoder::decode($decrypted);
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
return array(0, $result);
}
}