43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import base64
|
|
import hashlib
|
|
from contextlib import suppress
|
|
# from Cryptodome.Cipher import AES
|
|
|
|
class Decrypt :
|
|
def __init__(self, aid, episodeId, timestamp, nonce, userId, zid):
|
|
self._aid = aid
|
|
self._episodeId = episodeId
|
|
self._timestamp = timestamp
|
|
self._nonce = nonce
|
|
self._userId = userId
|
|
self._zid = zid
|
|
|
|
@classmethod
|
|
def get_aes(cls):
|
|
with suppress(AttributeError):
|
|
return cls.AES
|
|
try:
|
|
from Crypto.Cipher import AES
|
|
except ImportError:
|
|
raise ImportError("Missing optional dependency 'pycryptodomex'. Please install it to use this functionality.")
|
|
|
|
cls.AES = AES
|
|
return cls.AES
|
|
|
|
@classmethod
|
|
def _decrypt(cls, data: bytes, key: bytes, iv: bytes) -> bytes:
|
|
AES = cls.get_aes()
|
|
cipher = AES.new(key, AES.MODE_CBC, iv)
|
|
return cipher.decrypt(data)
|
|
|
|
def get_decrypt_infomations(self) -> tuple[bytes, bytes]:
|
|
|
|
temp_key = hashlib.sha256(f"{self._userId}{self._episodeId}{self._timestamp}".encode()).digest()
|
|
temp_iv = hashlib.sha256(f"{self._nonce}{self._timestamp}".encode()).digest()[:16]
|
|
encrypted_key = base64.b64decode(self._aid)
|
|
encrypted_iv = base64.b64decode(self._zid)
|
|
|
|
key = self._decrypt(encrypted_key, temp_key, temp_iv)[:16]
|
|
iv = self._decrypt(encrypted_iv, temp_key, temp_iv)[:16]
|
|
return key, iv
|