复制代码 隐藏代码 def v(e): t = []
for n in e:
if n == 'data':
data = e[n].copy()
for each in e[n]:
if not data[each]:
del data[each]
else:
data[each] = str(data[each])
t.append(n + '=' + json.dumps(data, separators=(',', ':')))
else:
t.append(n + '=' + str(e[n]))
t.append('key=NMVFVILMKT13GEMD3BKPKCTBOQBPZR2P')
return '&'.join(t)
复制代码 隐藏代码 def getZ(crypto): sign_data = bytes()
n = '1234567812345678'.encode()
sign_data += bytes([0, 8 * len(n)])
sign_data += n
sign_data += bytes.fromhex(crypto.ecc_table['a'])
sign_data += bytes.fromhex(crypto.ecc_table['b'])
sign_data += bytes.fromhex(crypto.ecc_table['g'])
sign_data += bytes.fromhex(crypto.public_key[2:])
return bytes.fromhex(sm3.sm3_hash(list(sign_data)))
# 可以写成固定值 # return bytes.fromhex('fde9a74125ca149ca75f4c2ccdaeed3e7d0b4b8c0f2c9e35530b9fe9a3ba1233')
复制代码 隐藏代码 def b(e, t): crypto = sm4.CryptSM4()
crypto.set_key(e[:16].encode(), sm4.SM4_ENCRYPT)
return crypto.crypt_ecb(t.encode()).hex().upper()[:16]
复制代码 隐藏代码 import requests_htmlimport randomimport timeimport jsonimport base64from Crypto.Hash import SHA256from gmssl import sm2, sm3, sm4, func publicKey = base64.b64decode("BEKaw3Qtc31LG/hTPHFPlriKuAn/nzTWl8LiRxLw4iQiSUIyuglptFxNkdCiNXcXvkqTH79Rh/A2sEFU6hjeK3k=".encode()).hex()
privateKey = base64.b64decode("AJxKNdmspMaPGj+onJNoQ0cgWk2E3CYFWKBJhpcJrAtC".encode()).hex()
appSecret = 'NMVFVILMKT13GEMD3BKPKCTBOQBPZR2P'appCode = 'T98HPCGN5ZVVQBS8LZQNOAEXVI9GYHKQ'def main(): requests = requests_html.HTMLSession()
key = b(appCode, appSecret).encode()
s = str(int(time.time()))
c = ''.join(random.choices("ABCDEFGHIJKLMNOPQRSTUVWXYZzbcdefghijklmnopqrstuvwxyz0123456789", k=8))
headers = {
'x-tif-paasid': 'undefined',
'"x-tif-timestamp': s,
'x-tif-nonce': c,
'"x-tif-signature': SHA256.new((s + c + s).encode()).hexdigest(),
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' }
data = {
'appCode': appCode,
'data': {
'addr': '',
'medinsLvCode': '',
'medinsName': '',
'medinsTypeCode': '',
'openElec': '',
'pageNum': 1,
'pageSize': 10,
'regnCode': '110000' },
'encType': 'SM4',
'signType': 'SM2',
'timestamp': int(s),
'version': '1.0.0' }
crypto = sm2.CryptSM2(privateKey, publicKey)
data['signData'] = base64.b64encode(bytes.fromhex(crypto.sign(bytes.fromhex(sm3.sm3_hash(list(getZ(crypto) + v(data).encode()))), func.random_hex(crypto.para_len)))).decode()
crypto = sm4.CryptSM4()
crypto.set_key(key, sm4.SM4_ENCRYPT)
data['data'] = {
'encData': crypto.crypt_ecb(json.dumps(data['data']).encode()).hex().upper()
}
data = {
'data': data
}
response = requests.post('https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/CommQuery/queryFixedHospital', json=data, headers=headers).json()
crypto = sm4.CryptSM4()
crypto.set_key(key, sm4.SM4_DECRYPT)
data = json.loads(crypto.crypt_ecb(bytes.fromhex(response['data']['data']['encData'])).decode())
print(data)def getZ(crypto): sign_data = bytes()
n = '1234567812345678'.encode()
sign_data += bytes([0, 8 * len(n)])
sign_data += n
sign_data += bytes.fromhex(crypto.ecc_table['a'])
sign_data += bytes.fromhex(crypto.ecc_table['b'])
sign_data += bytes.fromhex(crypto.ecc_table['g'])
sign_data += bytes.fromhex(crypto.public_key[2:])
return bytes.fromhex(sm3.sm3_hash(list(sign_data)))
# 可以写成固定值 # return bytes.fromhex('fde9a74125ca149ca75f4c2ccdaeed3e7d0b4b8c0f2c9e35530b9fe9a3ba1233')def v(e): t = []
for n in e:
if n == 'data':
data = e[n].copy()
for each in e[n]:
if not data[each]:
del data[each]
else:
data[each] = str(data[each])
t.append(n + '=' + json.dumps(data, separators=(',', ':')))
else:
t.append(n + '=' + str(e[n]))
t.append('key=' + appSecret)
return '&'.join(t)def b(e, t): crypto = sm4.CryptSM4()
crypto.set_key(e[:16].encode(), sm4.SM4_ENCRYPT)
return crypto.crypt_ecb(t.encode()).hex().upper()[:16]if __name__ == '__main__':
main()
侵权请私聊公众号删文
热文推荐