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;
}
大概浏览一下,应该是aad没用上吧~
$ciphertext = openssl_encrypt($real_plaintext, 'aes-256-gcm', $real_key, OPENSSL_RAW_DATA, $real_iv, $tag, $real_aad, 16);