关于签名
signature 生成规则
将 body 体
转成 json 字符串 + md5Key ,然后使用 MD5 进行加密
例如:
str = {"name":"xxx","age":"18"}123453333
md5(str)
repData 生成规则
同理 将 body 体
转成 json 字符串 ,然后使用 RSA 公钥进行加密,得到加密字符串,再 base64 编码
例如:
def encrypt(message: str, pub_key: str) -> str | None:
try:
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key.encode())
message_bytes = message.encode()
chunks = [message_bytes[i:i + CHUNK_SIZE] for i in range(0, len(message_bytes), CHUNK_SIZE)]
crypto = b"".join([rsa.encrypt(chunk, pubkey) for chunk in chunks])
return base64.b64encode(crypto).decode()
except Exception as e:
print(f"Error encrypting message: {e}")
return None
调用示例
注意是
application/x-www-form-urlencoded
格式 非 application/json
curl -X POST https://test.xxx.com/api/mastercard/holderQuery.html \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "agentId=888666000100201&reqData=2%2FCgBnwbtXQRR2XVmLu9uOiBCfjnd1pwq59LtPhufF6VtjbKcArfHE9Cqff575EFbIEBanPKlXbilnJj2Gz4AnEs1BOZOUEO85UIcD7YBBXdt1BC3vX8hzWXrsnPbC1ZW4589r7B40h0lL9NAOjs%2BJaDIN3nmpdqvFHJ4hF1rb0%3D&signature=e5c3ca8eba4de85f44262f8d34ac21ee"
返回示例
正常所有接口数据都需要使用 RSA 私钥进行解密 服务端返回的数据
{"respCode":"00","respMsg":"","respData":"nnTq5O+k89BsneEN1+SUKxj6PlNSsBzeF29IeEkNQjWrRATjW+dtYPONKFO9YfG5dlLg1qZH5PBRbEWB44Q9wmvT5PLoJNdM5Uk1zhhLQ0sJA186BSiwxwMYty0wgWgC6lsY4QQzuNR/aAsr8DxLR7vsTxaPB3m5qAzO8i7D/Xu22OX52CxxDeNizXwg1+0iyKU6S5AOEKCkF52iJY8OucVOt2sq9q1aX2wBijG12ZWyuh6iVHMtJxrxEOTYpp1h481ixBvt809hte2J26mn0hbWPW8v14X/LsS+5Mf5/0CcHm72tZ+EELyeDGI4TeZQeTw3Yf97KZfQ83cO15Ggzg=="}
解密示例:
def decrypt(encrypted_data: str, private_key: str) -> str:
private_key_bytes = private_key.encode('utf-8')
encrypted_data = base64.b64decode(encrypted_data)
# Deserialize the private key from PEM format
private_key_obj = serialization.load_pem_private_key(private_key_bytes, password=None, backend=default_backend())
# Determine the appropriate chunk size based on the private key size
key_size_bytes = private_key_obj.key_size // 8 # Convert bits to bytes
decrypted_data = b""
# Decrypt the data in chunks based on the key size
for offset in range(0, len(encrypted_data), key_size_bytes):
chunk = encrypted_data[offset:offset + key_size_bytes]
decrypted_chunk = private_key_obj.decrypt(chunk, padding.PKCS1v15())
decrypted_data += decrypted_chunk
return decrypted_data.decode('utf-8')
Updated 4 months ago