# Python

# Python(3.7)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
# install requirement
pip install requests
pip install pycryptodome

"""

import requests
import json
import base64
from Crypto.Cipher import AES
from Crypto import Random
import time

# 请修改账号信息
SNUSER = "XXX"
SNKEY = "XXX"


def aes_encrypt_seg(content):
    remainder = len(content) % AES.block_size
    if remainder:
        padded_value = content + '\0' * (AES.block_size - remainder)
    else:
        padded_value = content
    # a random 16 byte key
    iv = Random.new().read(AES.block_size)
    # CFB mode
    cipher = AES.new(bytes(SNKEY, encoding='utf8'), 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(content)])
    ciphertext = iv + value
    return str(base64.encodebytes(ciphertext).strip(), encoding="utf8")


def aes_decrypt_seg(content):
    data = base64.decodebytes(bytes(content, 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(SNKEY, encoding="utf8"), 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 TestCheck(data):
    POST_URL = "https://ipdata.yazx.com/api/ip/v2/check/"
    # user list origin text
    pstr = json.dumps(data)
    # encrypt the origin text
    cstr = aes_encrypt_seg(pstr)

    payload = {
        "snuser": SNUSER,
        "data": cstr
    }
    r = requests.post(POST_URL, data=json.dumps(payload), verify=True)
    rjson = json.loads(r.text)
    if rjson["status"] == 200:
        print("get status success =>>>")
        print(aes_decrypt_seg(rjson["data"]))
    else:
        print("get status failed")


if __name__ == "__main__":
    data = {"ip": "122.241.179.74", "attack_time": 1663320929}
    TestCheck(data)
Last Updated: 2/29/2024, 4:08:11 PM