1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import base64 import ctypes import hashlib
from cryptography.hazmat.primitives.ciphers import Cipher from cryptography.hazmat.primitives.ciphers import algorithms from cryptography.hazmat.primitives.ciphers import modes from cryptography.hazmat.primitives import padding
def parse_cipher_text(content): content = base64.b64decode(content) result = [] for i in range(0, len(content), 4): result.append(int.from_bytes(content[i: i + 4], byteorder='big', signed=True)) return result[2:4], result[4:]
def md5_encrypt(content): """ MD5内容摘要 :param content: :return: """ if isinstance(content, str): content = content.encode('utf8') m = hashlib.md5() m.update(content) return m.hexdigest()
def aes_decrypt(content, key, iv): """ AES CBC PKCS7 padding :param iv: :param key: :param content: :return: """ if not isinstance(content, bytes): content = content.encode()
cipher = Cipher(algorithm=algorithms.AES(key), mode=modes.CBC(iv)) decryptor = cipher.decryptor() content = decryptor.update(content)
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder() return unpadder.update(content) + unpadder.finalize()
def array_to_bytes(array): result = b'' for arr in array: result += arr.to_bytes(length=4, byteorder='big', signed=True) return result
def generate_aes_key_iv(salt): result = [] md5_result = md5_encrypt(b'500 Server error' + array_to_bytes(salt)) for i in range(0, len(md5_result), 8): result.append(ctypes.c_int32(int(md5_result[i: i+8], 16)).value) md5_result = md5_encrypt(array_to_bytes(result) + b'500 Server error' + array_to_bytes(salt)) for i in range(0, len(md5_result), 8): result.append(ctypes.c_int32(int(md5_result[i: i+8], 16)).value) md5_result = md5_encrypt(array_to_bytes(result[4:]) + b'500 Server error' + array_to_bytes(salt)) for i in range(0, len(md5_result), 8): result.append(ctypes.c_int32(int(md5_result[i: i+8], 16)).value) return array_to_bytes(result[:8]), array_to_bytes(result[8:])
def run(): cipher_text = "U2FsdGVkX1/GN1cLWc8heoJDQNxNpcASTl/CnjDuARekPQqjXk/8jFgs1eDbcBbpCGXY6GIdZsntUXKaA64gmg==" salt, cipher_text = parse_cipher_text(content=cipher_text) key, iv = generate_aes_key_iv(salt=salt) content = aes_decrypt(content=array_to_bytes(cipher_text), key=key, iv=iv).decode() print(content)
if __name__ == '__main__': run()
|