必须定义py blowflish PY_SSIZE_T_CLEAN 宏

问题描述 投票:0回答:1

我将收到一个带有密钥的包裹,我需要用河豚解密它,但我收到此错误:

SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

关于此功能:

def encrypt_blowfish(key, plaintext):
    cipher = Blowfish.new(key, Blowfish.MODE_ECB)
    plaintext_padded = pad_data(plaintext, 8)  # 8 é o tamanho do bloco para Blowfish
    ciphertext = cipher.encrypt(plaintext_padded)
    return ciphertext

创建河豚时

这是代码:

def pad_data(data, block_size):
    padding_len = block_size - (len(data) % block_size)
    padding = bytes([padding_len]) * padding_len
    return data + padding

def encrypt_blowfish(key, plaintext):
    cipher = Blowfish.new(key, Blowfish.MODE_ECB)
    plaintext_padded = pad_data(plaintext, 8)  # 8 é o tamanho do bloco para Blowfish
    ciphertext = cipher.encrypt(plaintext_padded)
    return ciphertext

def main():
    host = 'x.x.x.x'
    port = 2106
    login = 'x'
    senha = 'x'

    # Configurar o socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((host, port))

    # Etapa 1: Enviar RequestAuthLogin
    request_auth_login = b'\x08' + b'\x32\x00\x00' + b'\x08\x00\x00' + login.encode() + b'\x00\x00\x00\x00'
    request_auth_login = len(request_auth_login).to_bytes(2, byteorder='little') + request_auth_login
    client_socket.send(request_auth_login)
    print('Enviado RequestAuthLogin')

    # Receber e decifrar o RSA Key
    rsa_key = client_socket.recv(128)
    rsa_key_decrypted = encrypt_blowfish(b'5F3B352E5D39342D33313D3D2D257854215E5B24\x00', rsa_key)
    print('RSA Key recebida e decifrada:', rsa_key_decrypted.hex())
    client_socket.close()

if __name__ == '__main__':
    main()
python-3.x blowfish
1个回答
0
投票

您遇到的错误消息“SystemError:必须为 '#' 格式定义 PY_SSIZE_T_CLEAN 宏”,表明您正在使用的 Python 环境存在问题。此错误与 Python 版本及其处理某些格式化宏的方式有关。

要解决此问题,您可以尝试以下步骤:

  • 更新 Python:确保您使用的 Python 版本与您正在使用的 Blowfish 库兼容。此错误可能与使用较旧的 Python 版本有关。

  • 定义 PY_SSIZE_T_CLEAN:在导入 Blowfish 模块之前,在代码开头添加以下行来定义 PY_SSIZE_T_CLEAN:

这应该添加到代码的开头

import os

os.environ["PY_SSIZE_T_CLEAN"] = "1"

  • 确保依赖项:确保安装了必要的依赖项。您应该安装 pycryptodome 库才能使用 Blowfish 加密。您可以使用 pip 安装它:

    pip 安装 pycryptodome

进行这些更改后,尝试再次运行您的代码。

© www.soinside.com 2019 - 2024. All rights reserved.