审查视图

simplewind/extend/wxapp/aes/Prpcrypt.php 1.5 KB
董瑞恩 authored
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
<?php
namespace wxapp\aes;

/**
 * Prpcrypt class
 *
 *
 */
class Prpcrypt
{
    public $key;

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

    /**
     * 对密文进行解密
     * @param string $aesCipher 需要解密的密文
     * @param string $aesIV 解密的初始向量
     * @return string 解密得到的明文
     */
    public function decrypt($aesCipher, $aesIV)
    {
董瑞恩 authored
27
        if (function_exists('mcrypt_module_open')) {
董瑞恩 authored
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 54 55 56 57 58 59 60
            try {

                $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

                mcrypt_generic_init($module, $this->key, $aesIV);

                //解密
                $decrypted = mdecrypt_generic($module, $aesCipher);
                mcrypt_generic_deinit($module);
                mcrypt_module_close($module);
            } catch (\Exception $e) {
                return [ErrorCode::$IllegalBuffer, null];
            }
        } else if (function_exists('openssl_decrypt')) {

            $decrypted = openssl_decrypt($aesCipher, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $aesIV);

            if ($decrypted === false) return [ErrorCode::$IllegalBuffer, null];
        }


        try {
            //去除补位字符
            $pkc_encoder = new PKCS7Encoder;
            $result      = $pkc_encoder->decode($decrypted);

        } catch (\Exception $e) {
            //print $e;
            return [ErrorCode::$IllegalBuffer, null];
        }
        return [0, $result];
    }
}