WhatsApp FLows - 无法解密从服务器收到的响应

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

我正在尝试使用 WhatsApp 业务 API 和以下代码生成 WhatsApp 流程的响应

解密部分运行正常,但是当我尝试发送响应时,我收到错误:“无法解密从服务器收到的响应。”

我已参考此处的文档,但我仍在努力寻找生成和验证响应的正确方法。

是否有人有此 API 的经验或可以提供有关如何正确格式化和发送响应的指导?任何示例或相关资源的链接将不胜感激。



def post(self, request, *args, **kwargs):
        try:
            dict_data = json.loads(request.body.decode('utf-8'))
            encrypted_flow_data_b64 = dict_data['encrypted_flow_data']
            encrypted_aes_key_b64 = dict_data['encrypted_aes_key']
            initial_vector_b64 = dict_data['initial_vector']
            
            flipped_iv = self.flip_iv(initial_vector_b64.encode('utf-8'))
            
            encrypted_aes_key = b64decode(encrypted_aes_key_b64)
            key_private = open('*******.pem', 'rb').read().decode('utf-8')
            private_key = load_pem_private_key(key_private.encode('utf-8'), password="*************".encode('utf-8'))
            
            aes_key = private_key.decrypt(encrypted_aes_key, OAEP(mgf=MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))
            aes_key_b64 = b64encode(aes_key).decode('utf-8')
            
            flow_data  = b64decode(encrypted_flow_data_b64)
            key = b64decode(aes_key_b64)
            iv = b64decode(initial_vector_b64)
            
            encrypted_flow_data_body = flow_data[:-16]
            encrypted_flow_data_tag = flow_data[-16:]
            cipher = Cipher(algorithms.AES(key), modes.GCM(iv,encrypted_flow_data_tag))
            decryptor = cipher.decryptor()
            decrypted_data = decryptor.update(encrypted_flow_data_body) + decryptor.finalize()
            flow_data_request_raw = decrypted_data.decode("utf-8")
            
            hello_world_text = "HELLO WORLD"
            
            response_data = {
                "version": "3.0",
                "screen": "MY_FIRST_SCREEN",
                "data": {
                    "hello_world_text": hello_world_text
                }
            }

            response_json = json.dumps(response_data)
            
            # Obtendo a chave AES após descriptografar encrypted_aes_key
            fb_aes_key = private_key.decrypt(encrypted_aes_key, OAEP(mgf=MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))

            # Usando a chave AES para criptografar a resposta
            response_cipher = Cipher(algorithms.AES(fb_aes_key), modes.GCM(iv))
            encryptor = response_cipher.encryptor()
            encrypted_response = (
                encryptor.update(response_json.encode("utf-8")) +
                encryptor.finalize() +
                encryptor.tag
            )
            encrypted_response_b64 = b64encode(encrypted_response).decode("utf-8")
            
            # Construct the final response
            final_response = {
                "encrypted_flow_data": encrypted_response_b64,
                "encrypted_aes_key": encrypted_aes_key_b64,
                "initial_vector": initial_vector_b64
            }
            
            return JsonResponse(final_response, status=200)
        except Exception as e:
            print(e)
            return HttpResponse(status=500, content='ok')

    
    def flip_iv(self, iv):
        flipped_bytes = []
        for byte in iv:
            flipped_byte = byte ^ 0xFF
            flipped_bytes.append(flipped_byte)
        return bytes(flipped_bytes)
        ```



The entire decoding part is working normally but when returning the response I receive the error "Could not decrypt the response received from the server.
"I can't find how to send the correct answer or how to validate it. The documentation can be found at https://developers.facebook.com/docs/whatsapp/flows/reference/implementingyourflowendpoint#data_exchange_request

Can anyone help me or show me a link I can test?
python django whatsapp whatsapp-cloud-api whatsapp-flows
1个回答
0
投票

我发现你的代码有一些问题

  1. 在base64解码后需要像这样翻转iv
iv = b64decode(initial_vector_b64)
flipped_iv = flip_iv(iv)
  1. 在加密中使用 Flipped_iv 和名为
    key
    的变量(不是 fb_aes_key)
response_cipher = Cipher(algorithms.AES(key), modes.GCM(flipped_iv))
encryptor = response_cipher.encryptor()
encrypted_response = (
    encryptor.update(response_json.encode("utf-8")) +
    encryptor.finalize() +
    encryptor.tag
)
  1. 在正文中以纯文本形式返回响应
encrypted_response_b64 = b64encode(encrypted_response).decode("utf-8")
return HttpResponse(encrypted_response_b64, content_type='text/plain')
© www.soinside.com 2019 - 2024. All rights reserved.