某航专业版返回数据解密

某航专业版返回数据解密

import base64
import zlib
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from Crypto.Cipher import PKCS1_v1_5
from Crypto import Random
from Crypto.PublicKey import RSA
import json


def rsa_decrypt(text_encrypted_base64: str):
    private_key = 'MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALl9ZyoHiDP+wEH7InONWgUwGEi3mfzOrZnBxSYp63nTqnM/h7JrqqHKe20yLUWNh3LQlGa6CTyFk1WgJZav1I++JgHwTkzDTTQHumSTuBVlKoBY6vXPteWKo0bp/lkRcCPtum+yS0AILjOHt3PUjr3G36fYtB8cJjx6NQIMwq3nAgMBAAECgYBRdYYRyh8BnpX2JADcOC5ZauavQJpKkMViadtXyD8WUGNhm3vPWkMkLFUJ/mgtTFs4i+swNfEKeCrdBqibRXqWKPoP/0/H6aY7tinTZL7euGmLdSfjK6e6yEf2joLOaaZqEOL+cUiVMPUs/TRXYzEcUnyv2YZzlxx3v/cNiJXTmQJBAPY/6dmgTrXzec2tCwa3rbKa5pKLZ4BKRo+DdaVkSIkUQ2sO47pO2WGTQMejuFZNArSlOCbEXVggg7HVndjYpNMCQQDA1Zp7fquYC46fAwJXbRYBa2QrOlQUkI6bCVeDJ8kRcAIjikcF4RuOc+aWeS5EMj2LByMi0TYWGkdhLWqOjrYdAkEA543uHgBKQ3oxnSAM8EIkCYDzM/X2W4J8m1Loe5+zPYNaaf9UUMr4SnKnRZNEMZK06p06EeowREA+1iijB1eglQJBALPwHkESiEhOsH0Mr8io0Bnu/f+gZAj0gz2EUBbgry0IWdaUF4YS8kXRPOA4n8WfI/JxbIX2twOU7fpuCLqRzLkCQQDRB7IOzD5JUwWwvpMNW3OqXEayw0uinEHKxUTYTuUQqmuhVp+DUb/Z9X7Ip3jPypVXDpUGIlT7POnHolnETIvn'
    private_key = [private_key[64 * _: 64 * _ + 64] for _ in range(0, len(private_key) // 64 + 1)]
    private_key = [_ for _ in private_key if _]
    private_key = '''-----BEGIN PRIVATE KEY-----\n''' + '\n'.join(private_key) + '''\n-----END PRIVATE KEY-----'''
    private_key = private_key.encode()
    # 字符串指定编码(转为bytes)
    text_encrypted_base64 = text_encrypted_base64.encode('utf-8')
    # base64解码
    text_encrypted = base64.b64decode(text_encrypted_base64)
    # 构建私钥对象
    cipher_private = PKCS1_v1_5.new(RSA.importKey(private_key))
    # 解密(bytes)
    text_decrypted = cipher_private.decrypt(text_encrypted, Random.new().read)
    return base64.b64encode(text_decrypted).decode()


def decrypt_aes_cbc(ciphertext, key, iv):
    key = base64.b64decode(key)
    iv = base64.b64decode(iv)
    ciphertext = base64.b64decode(ciphertext)
    backend = default_backend()
    cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
    decryptor = cipher.decryptor()
    padded_data = decryptor.update(ciphertext) + decryptor.finalize()
    unpadder = padding.PKCS7(128).unpadder()
    data = unpadder.update(padded_data) + unpadder.finalize()
    return decompress_data(data)


def decompress_data(data):
    try:
        decompressed_data = zlib.decompress(data, -zlib.MAX_WBITS)
        return json.loads(decompressed_data.decode())
    except Exception as e:
        print("解压缩失败:", e)
        return None


if __name__ == '__main__':
    text = 'eyJkIjoiZXlKcGRpSTZJbEp4WVdoQmNETlVSRkI2WlRSSE1YbE1Ta3hvTUZFOVBTSXNJblpoYkhWbElqb2lLMlJ5TDNWWGEzbHVTVE5XTXpGa1ZqWTFUVzl2Wmxkc1RVUnZMMWxoU0d0c1pVWm9abkJvTDA1TVJYUlNjVkY1VGpJdk1VTTNkMUU1Ym1aQlpGSlVRemRLWW5VMFNtcFZRbFZOYTJOdWQyTmlaakpWWTFkbGFXeFZWQzlSYXpCTGFUWTNUMWwwTUVVeFltSlZVR2cxUm1sMVNYSnRlVGRGVW5NMWNucEdjelJuVlhWemRYUnRPRkp1TmxjNWFWTkZhREp5WkU5QmJuSkVXVkpzZVhsWWNWZHNNbTVtUkRGUVRtMHdiMW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swWT0ifQ=='
    json_data = json.loads(base64.b64decode(text).decode())
    key = rsa_decrypt(json_data['k'])
    toJsonData = json.loads(base64.b64decode(json_data['d']).decode())
    value = toJsonData['value']
    iv = toJsonData['iv']
    decrypted_message = decrypt_aes_cbc(value, key, iv)
    print(decrypted_message)

某航专业版返回数据解密_第1张图片

你可能感兴趣的:(python,python)