PHP 微信v3 敏感信息加密

发布时间 2023-10-11 09:30:37作者: -韩
    protected static function getEncrypt($str)
    {
        $public_key = config('wx.platformCertificateFilePath');//平台证书,如果是地址,可以使用file_get_content来获取内容
        $pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
        if (empty($pu_key))
            throw new Exception('无法获取平台证书');

        $encrypted = '';
        if (openssl_public_encrypt($str, $encrypted, $pu_key, OPENSSL_PKCS1_OAEP_PADDING)) {
            //base64编码
            $encrypted = base64_encode($encrypted);
        } else {
            throw new Exception('加密失败');
        }
        return $encrypted;
    }