AES加密解密

追梦nan發表於2024-04-15
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64

# 加密資料 AES-128 16位位元組

key = b'1111111111111111'
# key = '1111111111111111'.encode()
iv = b'1111111111111111'
# 建立例項
aes = AES.new(key, AES.MODE_CBC, iv)
# 要加密的內容
text = b"I am zbb"
# 填充資料16
data = pad(text, AES.block_size)
# 加密
encrypted = aes.encrypt(data)
# 處理編碼
base64_data = base64.b64encode(encrypted).decode('utf-8')
print(base64_data)

#############################解密  上面和蝦米分開
# 處理解碼
new_data = base64.b64decode('BGfPk3vh6JNJKbdz4rdvIQ==')
# 解密
data = aes.decrypt(new_data)
data = unpad(data, 16).decode('utf-8')
print(data)

#############爬蟲案例

import json
import time
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
import requests

cookies = {
    'SECKEY_ABVK': '4FEyWha4eeyQRebV3Rk5zI+GJ2wPbhOvft92A0pnZOM2rPb4y/7LT74yIAqkGHtC1SJDxiTO0DVfwxALhYNENA%3D%3D',
    'Hm_lvt_6cd598ca665714ffcd8aca3aafc5e0dc': '1713113457',
    'Hm_lpvt_6cd598ca665714ffcd8aca3aafc5e0dc': '1713113457',
    'BMAP_SECKEY': '4FEyWha4eeyQRebV3Rk5zI-GJ2wPbhOvft92A0pnZOOJ6AQsDtC72oCm6kVt3nzu0h2wwN3496nmDWjq0gceSvHIyxIoM2MAuG9cKiJn7mpYRezaNB3cgCujWz1mvOUTfwV00H0nFrmyzuQ_QDX5Y75Ny9QgpmEzfm6KIqeEw8BDh2BheEB7aOsbMRNYBp_UzQb1G8R3ta5MREFaspIypA',
    'PHPSESSID': 'ab5civinlpujapg5lr4beumlne',
}

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json; charset=UTF-8',
    # 'Cookie': 'SECKEY_ABVK=4FEyWha4eeyQRebV3Rk5zI+GJ2wPbhOvft92A0pnZOM2rPb4y/7LT74yIAqkGHtC1SJDxiTO0DVfwxALhYNENA%3D%3D; Hm_lvt_6cd598ca665714ffcd8aca3aafc5e0dc=1713113457; Hm_lpvt_6cd598ca665714ffcd8aca3aafc5e0dc=1713113457; BMAP_SECKEY=4FEyWha4eeyQRebV3Rk5zI-GJ2wPbhOvft92A0pnZOOJ6AQsDtC72oCm6kVt3nzu0h2wwN3496nmDWjq0gceSvHIyxIoM2MAuG9cKiJn7mpYRezaNB3cgCujWz1mvOUTfwV00H0nFrmyzuQ_QDX5Y75Ny9QgpmEzfm6KIqeEw8BDh2BheEB7aOsbMRNYBp_UzQb1G8R3ta5MREFaspIypA; PHPSESSID=ab5civinlpujapg5lr4beumlne',
    'Origin': 'https://www.maomaozu.com',
    'Pragma': 'no-cache',
    'Referer': 'https://www.maomaozu.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

# 加密資料 AES-128 16位位元組


# 要加密的內容
for page in range(1, 5):
    text = {"Type": 0,
            "page": page,
            "expire": 1713114839095}
    text = json.dumps(text).encode('utf-8')
    # 填充資料16
    data = pad(text, AES.block_size)

    key = '55b3b62613aef1a0'.encode()
    iv = key
    # 建立例項
    aes = AES.new(key, AES.MODE_CBC, iv)
    # 加密
    encrypted = aes.encrypt(data)
    # 處理編碼
    base64_data = base64.b64encode(encrypted).decode('utf-8')
    # print(base64_data)

    response = requests.post('https://www.maomaozu.com/index/build.json', cookies=cookies, headers=headers,
                             data=base64_data)

    #############################解密  上面和蝦米分開

    key = '0a1fea31626b3b55'.encode()
    iv = key
    # 建立例項
    aes = AES.new(key, AES.MODE_CBC, iv)
    # 處理解碼
    new_data = base64.b64decode(response.text)
    # 解密
    data = aes.decrypt(new_data)
    data = unpad(data, 16).decode('utf-8')
    print(data)
    time.sleep(2)

# 關鍵字搜尋  路徑搜尋  方法搜尋decrypt(

相關文章