PHP 微信v3 敏感信息解密

发布时间 2023-10-11 09:35:45作者: -韩
    protected static function getDecrypt($encrypted)
    {
        $private_key = config('wx.merchantPrivateKeyFilePath'); //商户私钥 如果是路劲,可以使用file_get_content来获取
        $pi_key = openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
        if (empty($pi_key))
            throw new Exception('无法获取私钥');

        $decrypted = '';
        //私钥解密
        if ( openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key, OPENSSL_PKCS1_OAEP_PADDING)) {

            return $decrypted;
        } else {
            throw new Exception('解密失败');
        }
   
    }