-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
148 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import base64 | ||
import hashlib | ||
import json | ||
import struct | ||
import time | ||
|
||
from Crypto.Cipher import AES | ||
from Crypto.Util.Padding import pad | ||
|
||
|
||
class EncryptHelper: | ||
words = [1735160886, 1748382068, 1631021929, 1936684855] | ||
key_bytes = b''.join(struct.pack('>I', word) for word in words) | ||
iv = b'4hrivgw5s342f9b2' | ||
|
||
@staticmethod | ||
def get_md5(url: str) -> str: | ||
""" | ||
根据传入的url和params生成MD5摘要 | ||
:param url: API的url | ||
:return: MD5摘要 | ||
""" | ||
md5_hash = hashlib.md5(('url=' + url).encode('utf-8')).hexdigest() | ||
return md5_hash | ||
|
||
@staticmethod | ||
def encrypt_text(text: str) -> str: | ||
""" | ||
根据传入的text生成AES加密后的内容,并将其转为base64编码 | ||
:param text: 需要加密的字符串 | ||
:return: 加密后的base64编码字符串 | ||
""" | ||
text_encoded = base64.b64encode(text.encode()) | ||
|
||
cipher = AES.new(EncryptHelper.key_bytes, AES.MODE_CBC, EncryptHelper.iv) | ||
ciphertext = cipher.encrypt(pad(text_encoded, AES.block_size)) | ||
ciphertext_base64 = base64.b64encode(ciphertext).decode() | ||
|
||
return ciphertext_base64 | ||
|
||
@staticmethod | ||
def base64_to_hex(encoded_data): | ||
decoded_data = base64.b64decode(encoded_data) | ||
hex_string = ''.join([format(byte, '02x') for byte in decoded_data]) | ||
return hex_string | ||
|
||
@staticmethod | ||
def encrypt_payload(payload: str) -> str: | ||
""" | ||
把小红书加密参数payload转16进制 再使用base64编码 | ||
:param payload: 要加密处理的payload内容 | ||
:return: 加密后并进行base64编码的字符串 | ||
""" | ||
obj = { | ||
"signSvn": "55", | ||
"signType": "x2", | ||
"appID": "xhs-pc-web", | ||
"signVersion": "1", | ||
"payload": EncryptHelper.base64_to_hex(payload) | ||
} | ||
return base64.b64encode(json.dumps(obj, separators=(',', ':')).encode()).decode() | ||
|
||
@staticmethod | ||
def encrypt_xs(url: str, a1: str, ts: str) -> str: | ||
""" | ||
将传入的参数加密为小红书的xs | ||
:param url: API请求的URL | ||
:param a1: 签名参数a1 | ||
:param ts: 时间戳 | ||
:return: 最终的加密签名字符串,前缀为“XYW_” | ||
""" | ||
text = (f'x1={EncryptHelper.get_md5(url)};' | ||
f'x2=0|0|0|1|0|0|1|0|0|0|1|0|0|0|0|1|0|0|0;' | ||
f'x3={a1};' | ||
f'x4={ts};') | ||
return 'XYW_' + EncryptHelper.encrypt_payload(EncryptHelper.encrypt_text(text)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
loguru | ||
openpyxl | ||
requests | ||
pandas | ||
PyExecJS | ||
beautifulsoup4 | ||
pillow | ||
loguru~=0.7.2 | ||
openpyxl~=3.1.5 | ||
requests~=2.32.3 | ||
pandas~=2.2.2 | ||
PyExecJS~=1.5.1 | ||
beautifulsoup4~=4.12.3 | ||
pillow~=10.4.0 | ||
pycryptodome~=3.21.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters