Facing decryption error while decrypting encrypted payload using RSA Cipher Algorithm for GST and E-Way Bill authentication

33 views Asked by At

I am currently facing an error when I try to fetch GST and E-way bill data and authenticate with the help of a payload using a public key and credentials. As part of the authentication process, I have encrypted the payload using Cipher Algorithm(RSA) and base64 encoding. However, when I attempt to decrypt the encrypted data, I encounter a decryption error.

Here's a breakdown of the steps I've taken:

  • I have a payload containing the necessary data for authentication.
  • I encrypted the payload using base64 encoding before sending it for authentication.
  • Upon receiving the response, I attempt to decrypt the encrypted payload using Python.

However, during the decryption process, I am encountering an error that prevents me from successfully decrypting the data.

I have verified that the encryption process is working correctly, as the encrypted payload appears to be in the expected format. However, I am unsure why I am encountering this decryption error.

please review the code:

@api_view(['POST'])
def gst_protal_security_token(request):
    try:
        public_key_file_path = os.path.abspath("sand_public_key.pem")
        if not os.path.exists(public_key_file_path):
            return Response({"status": "error", "message": "Public key file not found"})
        with open(public_key_file_path, 'rb') as file:
            public_key = RSA.import_key(file.read())
            # public_key = file.read()
        
        client_id = "***********"
        client_secret = "**********"
        gstin = "***********"
        user_name = "********"
        password = "**********"
        url = "https://einv-apisandbox.nic.in/eivital/v1.04/auth"
        action = "ACCESSTOKEN"
        cipher_rsa = PKCS1_OAEP.new(public_key) 
            
        session = requests.Session()
        headers = {
                "client-id": client_id,
                "client-secret": client_secret,
                "Gstin": gstin
        }
            
        # aes_key = generate_secure_key()
        aes_key = os.urandom(32)
        iv = os.urandom(16)

  
        auth_data = {
                "Action":action,
                "UserName": user_name,
                "Password": password,
                "AppKey": base64.b32encode(aes_key).decode(),
                "ForceRefreshAccessToken": True
        }
            

        encrypted_data = cipher_rsa.encrypt(json.dumps(auth_data).encode())
        payload = {"Data": base64.b64encode(encrypted_data).decode()}
     

        response = session.post(url, headers=headers, json=payload)
            
        if response.status_code == 200:
            verification = response.json()
            if 'Data' in verification and 'Sek' in verification['Data']:
                sek = decrypt_by_symmetric_key(verification['Data']['Sek'], aes_key)
            else:
                return Response({"status": "error", "message": "Missing 'data' or 'Sek' key in verification"})
        else:
            error_message = response.text
            return Response({"status": "error", "message": error_message})
    except Exception as ex:
        return Response({"status": "error", "message": str(ex)})
0

There are 0 answers