如何使用 GramJS(电报引擎)接受秘密聊天(电报)?

问题描述 投票:0回答:0

我正在使用 GramJS 编写一个可以在 Telegram 中接受秘密聊天的服务,但是我遇到了一个错误。

如果有人向您发送秘密聊天请求,它看起来像这样:

{
  CONSTRUCTOR_ID: 3030575245,
  SUBCLASS_OF_ID: 2676568142,
  className: 'UpdateEncryption',
  classType: 'constructor',
  chat: {
    CONSTRUCTOR_ID: 1223809356,
    SUBCLASS_OF_ID: 1831379834,
    className: 'EncryptedChatRequested',
    classType: 'constructor',
    flags: 0,
    folderId: null,
    id: NEGATIVENUMBER,
    accessHash: Integer { value: BIGINT },
    date: 1678550555,
    adminId: Integer { value: BIGINT },
    participantId: Integer { value: BIGINT},
    gA: BUFFER
  },
  date: 1678550555
}

根据文档我的代码应该是这个样子:

const result = await this.telegramClientService.client.invoke(
  new Api.messages.AcceptEncryption({
    peer: new Api.InputEncryptedChat({
      accessHash: chat.accessHash,
      chatId: chat.id,
    }),
    gB: Buffer.from('some bytes here'),
    keyFingerprint: chat.accessHash,
  }),
);

除非这不起作用,我收到以下错误,该错误未在文档的可能错误部分中列出:

RPCError: 400: DH_G_B_INVALID (caused by messages.AcceptEncryption)
    at RPCMessageToError (REDACTED\node_modules\telegram\errors\index.js:28:12)
    at MTProtoSender._handleRPCResult (REDACTED\node_modules\telegram\network\MTProtoSender.js:517:58)
    at MTProtoSender._processMessage (REDACTED\node_modules\telegram\network\MTProtoSender.js:442:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async MTProtoSender._recvLoop (REDACTED\node_modules\telegram\network\MTProtoSender.js:418:17) {
  code: 400,
  errorMessage: 'DH_G_B_INVALID'
}

为了解决这个错误,我尝试计算 gB 值,但最终得到一个更大的值,导致错误。这就是我尝试计算 gB 值的方式:

private getGB(dhConfig: Api.messages.TypeDhConfig): bigint {
if (dhConfig.className === 'messages.DhConfigNotModified') {
  throw new Error('Unexpected DhConfigNotModified');
}

const g = BigInt(dhConfig.g);
const p = dhConfig.p.readBigUInt64BE();
const randBytes = randomBytes(16).readBigUInt64BE();

return g ** randBytes % p;
}
javascript typescript telegram diffie-hellman
© www.soinside.com 2019 - 2024. All rights reserved.