You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用NodeJS本地解密并验证Play Integrity API令牌

使用Node.js本地解密Play Integrity API令牌

要在Node.js环境中本地解密Play Integrity API的令牌,你可以借助Node.js内置的crypto模块完成,无需额外引入复杂第三方库。以下是完整实现步骤和代码示例:

准备工作

  • 从Google Cloud控制台下载你的服务账号JSON密钥文件(需拥有Play Integrity API权限)
  • 获取客户端传递的Play Integrity加密令牌(即integrityToken)

实现步骤

1. 安装辅助包(可选)

如果需要便捷处理Base64URL编码,可安装base64url包:

npm install base64url

2. 编写解密代码

const crypto = require('crypto');
const base64url = require('base64url');
const fs = require('fs');

// 读取服务账号密钥
const serviceAccount = JSON.parse(fs.readFileSync('./service-account-key.json', 'utf8'));
const privateKey = serviceAccount.private_key;

async function decryptPlayIntegrityToken(integrityToken) {
  // 分割令牌的四个核心部分:加密密钥、IV、密文、认证标签
  const [encryptedKeyB64Url, ivB64Url, ciphertextB64Url, tagB64Url] = integrityToken.split('.');

  // 将Base64URL格式解码为Buffer
  const encryptedKey = base64url.toBuffer(encryptedKeyB64Url);
  const iv = base64url.toBuffer(ivB64Url);
  const ciphertext = base64url.toBuffer(ciphertextB64Url);
  const tag = base64url.toBuffer(tagB64Url);

  // 用服务账号私钥解密AES密钥(使用RSA-OAEP算法)
  const aesKey = crypto.privateDecrypt(
    {
      key: privateKey,
      padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
      oaepHash: 'sha256'
    },
    encryptedKey
  );

  // 用AES-GCM算法解密密文,需传入IV和认证标签
  const decipher = crypto.createDecipheriv('aes-256-gcm', aesKey, iv);
  decipher.setAuthTag(tag);

  let decrypted = decipher.update(ciphertext, null, 'utf8');
  decrypted += decipher.final('utf8');

  return JSON.parse(decrypted);
}

// 使用示例
const integrityToken = '你的加密令牌内容';
decryptPlayIntegrityToken(integrityToken)
  .then(verdict => console.log('解密后的完整性判定:', verdict))
  .catch(err => console.error('解密失败:', err));

关键注意事项

  • 确保服务账号已被授予Play Integrity Decrypter角色,否则无法完成解密操作
  • 令牌的四个部分必须严格按顺序分割,不能遗漏或错位
  • AES-GCM解密时必须正确传入认证标签,否则会抛出解密失败错误
  • 服务账号私钥需妥善保管,绝对不能泄露给第三方

内容的提问来源于stack exchange,提问作者Nafis Abdullah Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 13:35:20