# python
# python(3.7)
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
# install requirement
pip install requests
pip install pycryptodome
"""
import requests
import json
import hashlib
import base64
from Crypto.Cipher import AES
from Crypto import Random
POST_URL = "https://api.yazx.com/phone/check/v3/"
SNUSER = "xxx"
SNKEY = 'xxx'
def encrypt(encrypt_str: str, cecret: str):
"""
aes加密数据后,再进行base54编码后返回
:param encrypt_str:
:param cecret:
:return:
"""
remainder = len(encrypt_str) % AES.block_size
if remainder:
padded_value = encrypt_str + '\0' * (AES.block_size - remainder)
else:
padded_value = encrypt_str
# a random 16 byte key
iv = Random.new().read(AES.block_size)
# CFB mode
cipher = AES.new(bytes(cecret, encoding="utf-8"), AES.MODE_CFB, iv, segment_size=128)
# drop the padded value(phone number length is short the 16bytes)
value = cipher.encrypt(bytes(padded_value, encoding="utf8")[:len(encrypt_str)])
ciphertext = iv + value
return str(base64.encodebytes(ciphertext).strip(), encoding="utf8")
def decrypt(encrypt_str: str, cecret: str):
"""
base64解码后,再进行aes解密
:param encrypt_str:
:param cecret:
:return:
"""
data = base64.decodebytes(bytes(encrypt_str, encoding="utf8"))
cihpertxt = data[AES.block_size:]
remainder = len(cihpertxt) % AES.block_size
if remainder:
padded_value = cihpertxt + b'\0' * (AES.block_size - remainder)
else:
padded_value = cihpertxt
cryptor = AES.new(bytes(cecret, encoding="utf-8"), AES.MODE_CFB, data[0:AES.block_size], segment_size=128)
plain_text = cryptor.decrypt(padded_value)
return str(plain_text[0:len(cihpertxt)], encoding="utf8")
def check(country_code: str, national_number: str):
phone_list = []
phone_list.append({
"country_code": country_code,
"national_number_sha1": hashlib.sha1(national_number.encode()).hexdigest()
})
phone_list_str = json.dumps(phone_list)
print("====>request plain<====")
print(phone_list_str)
# encrypt the origin text
payload = {
"snuser": SNUSER,
"data": encrypt(phone_list_str, SNKEY)
}
print("====>request body<====")
print(payload)
r = requests.post(POST_URL, data=json.dumps(payload), verify=True)
rjson = json.loads(r.text)
print("====>response body<====")
print(rjson)
if rjson["status"] == 200:
print("====>response plain<====")
print(decrypt(rjson["data"], SNKEY))
if __name__ == "__main__":
check(country_code="86", national_number="13800000000")
# check(country_code="1", national_number="3333333333")