收藏
回答

服务端api加密的php版本,帮我看下错误在哪里?

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug 服务端api签名指南 微信安卓客户端 6.5.3 2.0.0
    const cipher = crypto.createCipheriv("aes-256-gcm", real_key, real_iv)
    cipher.setAAD(real_aad)

    let cipher_update = cipher.update(real_plaintext)
    let cipher_final = cipher.final()
    const real_ciphertext = Buffer.concat([cipher_update, cipher_final])
    const real_authTag = cipher.getAuthTag()
shs上面这段node.js代码变成php改怎么写啊。自己试了下是错误的,如标红的地方


    public function test()

    {

        $ctx = array(

            "local_sym_key" => "otUpngOjU+nVQaWJIC3D/yMLV17RKaP6t4Ot9tbnzLY=",

            "local_sym_sn" => "fa05fe1e5bcc79b81ad5ad4b58acf787",

            "local_appid" => "wxba6223c06417af7b",

            "url_path" => "https://api.weixin.qq.com/wxa/getuserriskrank"

        );

        $req = array(

            "appid" => "wxba6223c06417af7b",

            "openid" => "oEWzBfmdLqhFS2mTXCo2E4Y9gJAM",

            "scene" => 0,

            "client_ip" => "127.0.0.1",

        );

        $local_ts = floor(time());

        $nonce = str_replace("=", "",base64_encode(random_bytes(16)));

        $reqex = array(

            "_n" => $nonce,

            "_appid" => $ctx['local_appid'],

            "_timestamp" =>$local_ts

        );        

        $real_req             = array_merge($reqex, $req);

        $plaintext            = json_encode($real_req);

        $aad                  = $ctx['url_path'].'|'.$ctx['local_appid'].'|'.$local_ts.'|'.$ctx['local_sym_sn'];

        $real_key             = base64_decode($ctx['local_sym_key']);

        $real_iv              = random_bytes(12);

        $real_aad             = utf8_decode($aad);

        $real_plaintext       = utf8_decode($plaintext);

        $ciphertext           = openssl_encrypt($real_plaintext, 'aes-256-gcm', $real_key, $options=0, $real_iv, $tag);

        $iv = base64_encode($real_iv);

        $data = base64_encode($real_plaintext.$ciphertext);

        $authtag = base64_encode($ciphertext->getAuthTag());

        $req_data = [$iv, $data, $authtag];

        $new_req = array(

                "req_ts" => $local_ts,

                "req_data" => json_encode($req_data)

            );

        return $new_req;

    }

回答关注问题邀请回答
收藏

1 个回答

  • ؞咬了你؞F࿆ū࿆t࿆ū࿆r࿆ē࿆
    ؞咬了你؞F࿆ū࿆t࿆ū࿆r࿆ē࿆
    2023-08-14

    大概浏览一下,应该是aad没用上吧~

    $ciphertext = openssl_encrypt($real_plaintext, 'aes-256-gcm', $real_key, OPENSSL_RAW_DATA, $real_iv, $tag, $real_aad, 16);
    


    2023-08-14
    有用
    回复
登录 后发表内容