我在进行数据交换时遇到一个错误。我使用了 facebook 的 data_exchange 示例。
我收到此错误
我们无法解密您的有效负载。请检查您的加密/解密逻辑。
这是我的解密代码,它工作正常。
def decrypt_request(self,encrypted_flow_data_b64, encrypted_aes_key_b64, initial_vector_b64):
flow_data = b64decode(encrypted_flow_data_b64)
iv = b64decode(initial_vector_b64)
# Decrypt the AES encryption key
encrypted_aes_key = b64decode(encrypted_aes_key_b64)
PRIVATE_KEY = request.env['res.company'].search([],limit=1)
encoded_data = b64decode(PRIVATE_KEY.private_key_attachment).decode('utf-8').encode('utf-8')
private_key = load_pem_private_key(
encoded_data, password=None)
aes_key = private_key.decrypt(encrypted_aes_key, OAEP(
mgf=MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None))
# Decrypt the Flow data
encrypted_flow_data_body = flow_data[:-16]
encrypted_flow_data_tag = flow_data[-16:]
decryptor = Cipher(algorithms.AES(aes_key),
modes.GCM(iv, encrypted_flow_data_tag)).decryptor()
decrypted_data_bytes = decryptor.update(
encrypted_flow_data_body) + decryptor.finalize()
decrypted_data = json.loads(decrypted_data_bytes.decode("utf-8"))
return decrypted_data, aes_key, iv`
这是我的加密代码:
def encrypt_response(self,response, aes_key, iv):
# Flip the initialization vector
flipped_iv = bytearray(b ^ 0xFF for b in iv)
# Encrypt the response data
encryptor = Cipher(
algorithms.AES(aes_key),
modes.GCM(flipped_iv[:12]),
backend=default_backend()
).encryptor()
# Encrypt and finalize
response_bytes = json.dumps(response).encode("utf-8")
ciphertext = encryptor.update(response_bytes) + encryptor.finalize()
# Combine ciphertext and tag
encrypted_payload = ciphertext + encryptor.tag
# Encode as base64
encoded_payload = b64encode(encrypted_payload).decode("utf-8")
return encoded_payload
这是主要方法:
def whatsapp_data_exchange_flow(self,**kw):
data = json.loads(request.httprequest.data)
# Read the request fields
encrypted_flow_data_b64 = data['encrypted_flow_data']
encrypted_aes_key_b64 = data['encrypted_aes_key']
initial_vector_b64 = data['initial_vector']
decrypted_data, aes_key, iv = self.decrypt_request(
encrypted_flow_data_b64, encrypted_aes_key_b64, initial_vector_b64)
print(decrypted_data)
# Return the next screen & data to the client
response = {
"version": "3.0",
"screen": "BOOKING_INFO"
}
encrpted_response = self.encrypt_response(response, aes_key, iv)
headers = werkzeug.datastructures.Headers(None)
headers['Content-Type'] = 'text/plain'
response = request.make_response(final_response,headers=headers.to_wsgi_list(),status=200)
return response
问题出在你的
encrypt_response
函数中,你对初始向量 (IV) 做了不必要的修改。WhatsApp 的数据交换流程要求在加密和解密过程中使用相同的 IV。通过对 IV 进行异或操作,你改变了它,导致 WhatsApp 无法解密你的响应。
以下是修改后的
encrypt_response
函数:
def encrypt_response(self,response, aes_key, iv):
# Encrypt the response data
encryptor = Cipher(
algorithms.AES(aes_key),
modes.GCM(iv), # 使用原始的 IV,不要进行修改
backend=default_backend()
).encryptor()
# Encrypt and finalize
response_bytes = json.dumps(response).encode("utf-8")
ciphertext = encryptor.update(response_bytes) + encryptor.finalize()
# Combine ciphertext and tag
encrypted_payload = ciphertext + encryptor.tag
# Encode as base64
encoded_payload = b64encode(encrypted_payload).decode("utf-8")
return encoded_payload
通过删除
flipped_iv
并直接在
modes.GCM()
中使用原始的
iv
,你的加密和解密过程将使用相同的 IV,从而解决“无法解密的有效负载”错误。
请确保在测试代码之前重新启动你的应用程序,以使更改生效。
标签:python,whatsapi,data-exchange,whatsapp-flows From: 78505560