from Crypto.PublicKey import RSA
from base64 import b64encode, b64decode
import datetime
import pytz
import json
import requests
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
from Crypto.Cipher import PKCS1_OAEP
ist = pytz.timezone('Asia/Kolkata')
now = datetime.datetime.now(ist)
timestamp = now.strftime('%Y%m%d%H%M%S%z')
# print('Timestamp:', timestamp)
url = "https://test.proteangsp.co.in/gus/irp/nic/eivital/v1.04/auth"
asp_id = "1234"
username = "xyz"
password = "xxx"
gstin = "xzy"
X_ASP_AUTH_TOKEN = f'v2.0:{asp_id}:::{timestamp}:{gstin}:eInvoiceGeneration'
print(X_ASP_AUTH_TOKEN)
with open('./einv_sandbox_public_key.pem', mode='rb') as file:
public_key = RSA.import_key(file.read())
`your text`
在这里输入
with open('./privatekey.txt', mode='rb') as file:
private_key = RSA.import_key(file.read())
private_key_bytes = private_key.export_key()
rsa_key = RSA.importKey(private_key_bytes)
hash_obj = SHA256.new(X_ASP_AUTH_TOKEN.encode('utf-8'))
signature = pkcs1_15.new(private_key).sign(hash_obj)
headers = {
'Gstin': gstin,
'Content-type': 'application/json',
'X-Asp-Auth-Token': X_ASP_AUTH_TOKEN,
"X-Asp-Auth-Signature": b64encode(signature).decode('utf-8'),
}
initial_payload = {
"UserName": username,
"Password": password,
"AppKey": 'e1d65bgSeTrTatc7atLhKWyUbM/ekfbAWu2dFMfyNuYS+==',
"ForceRefreshAccessToken": False,
}
initial_payload_str = json.dumps(initial_payload)
initial_payload_bytes = initial_payload_str.encode('utf-8')
print(initial_payload_bytes)
initial_payload_encoded= b64encode(initial_payload_bytes)
print(initial_payload_encoded)
cipher = PKCS1_OAEP.new(public_key)
encrypted_payload = cipher.encrypt(initial_payload_bytes)
# encrypted_payload = cipher.encrypt(initial_payload_encoded)
encrypted_payload_base64 = b64encode(encrypted_payload)
raw_data = initial_payload
print(raw_data)
data = {
"Data": encrypted_payload_base64.decode('utf-8'),
}
print(data)
response = requests.post(url, headers=headers, data=json.dumps(data))
response_json = response.json()
print(response)
print(response_json)
我正在尝试验证获取身份验证令牌的 api。我有 PEM 格式的公钥和私钥(即 -----BEGIN PUBLIC KEY-----
sdasusa...-----结束公钥-----)。 api进程的条件是:
• 以上json payload需要转换成ByteArray。
• 使用Base64 编码器对该ByteArray 进行编码。
• 使用电子发票公钥加密编码输出。
• 注意:编码输出应为字节数组格式。
这是用于有效载荷数据
对于标头 Auth-token-signature
签署授权令牌
使用您生成的密钥对中的私钥(以及您发送给其的公钥
Protean)来签署授权令牌。必须将签名的身份验证令牌添加到请求中
密钥“X-Asp-Auth-Signature”下的标头
现在,当我运行代码时,我得到的响应是这样的