Gettig error when we do data_exchange in whatsapp flow

I face one error when i was doing data_exchange. I used facebook’s example of data_exchange.

I am getting this error

We failed to decrypt your payload. Please check your encryption/decryption logic.

Here it is my decryption code and it is working fine
`

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`

Here it is my encryption code
`

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

`

and this is main method
`

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`

New contributor

Mayur Nagar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật