29 lines
730 B
Python
29 lines
730 B
Python
|
|
import base64
|
||
|
|
import hashlib
|
||
|
|
import json
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
def decrypt_jd_message(encrypted_body: str, app_secret: str) -> dict | None:
|
||
|
|
try:
|
||
|
|
key = hashlib.md5(app_secret.encode("utf-8")).hexdigest()[:16].encode("utf-8")
|
||
|
|
raw = base64.b64decode(encrypted_body)
|
||
|
|
|
||
|
|
iv = raw[:16]
|
||
|
|
ciphertext = raw[16:]
|
||
|
|
|
||
|
|
from Crypto.Cipher import AES
|
||
|
|
|
||
|
|
cipher = AES.new(key, AES.MODE_CBC, iv)
|
||
|
|
decrypted = cipher.decrypt(ciphertext)
|
||
|
|
|
||
|
|
pad_len = decrypted[-1]
|
||
|
|
decrypted = decrypted[:-pad_len]
|
||
|
|
|
||
|
|
return json.loads(decrypted.decode("utf-8"))
|
||
|
|
except Exception:
|
||
|
|
logger.warning("JD message AES decrypt failed")
|
||
|
|
return None
|