python-3.x 解密加密文件时pypcryptodome(PKCS1_OAEP)出现问题:属性错误:“bytes”对象没有属性“n”

9nvpjoqh  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(488)

大家好阅读这条消息。
下面是我正在尝试做的事情的总结:
我想用python写一个程序,使用openssl来实现一个客户端-服务器程序,这个程序允许交换一个由对称加密方案保护的文件,这个文件的密钥本身是通过一个非对称加密方案安全地获得的。客户端将连接到服务器并检索服务器公钥,然后客户端将生成随机对称密钥并使用服务器的公钥加密该对称密钥,并且客户端将使用该对称密钥加密文件并将加密的文件和加密的服务器公钥发送到服务器,然后服务器将解密该文件。
我写了两端(服务器和客户端),但我得到了这个错误在服务器端:

Connection from ('192.168.1.52', 58686)
Traceback (most recent call last):
  File "/home/ubuntu/server5.py", line 38, in <module>
    decrypted_file = cipher.decrypt(encrypted_file)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/Crypto/Cipher/PKCS1_OAEP.py", line 161, in decrypt
    modBits = Crypto.Util.number.size(self._key.n)
AttributeError: 'bytes' object has no attribute 'n'

这是我的代码(请宽容一点,我是一个初学者,试图用python文档弄清楚所有这些)
客户端:

import socket
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import os

# Generate a random symmetric key
symmetric_key = os.urandom(16)

# Connect to the server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('192.168.1.53', 7777)
sock.connect(server_address)

# Receive the server public key
server_public_key_len = int.from_bytes(sock.recv(4), byteorder='big')
server_public_key = sock.recv(server_public_key_len)
server_rsa_key = RSA.import_key(server_public_key)

# Encrypt the symmetric key with the server public key
cipher = PKCS1_OAEP.new(server_rsa_key)
encrypted_key = cipher.encrypt(symmetric_key)

# Send the length of the encrypted key, then the encrypted key itself
sock.sendall(len(encrypted_key).to_bytes(4, byteorder='big'))
sock.sendall(encrypted_key)

# Encrypt the file with the symmetric key and send it to the server
filename = 'my_file'
with open(filename, 'rb') as f:
    plaintext = f.read()
cipher = PKCS1_OAEP.new(server_rsa_key)
ciphertext = cipher.encrypt(plaintext)
sock.sendall(ciphertext)
sock.close()

以及服务器端:

import socket
import os
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

# Generate RSA key pair for the server
key = RSA.generate(2048)

# Create a TCP/IP socket for the server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to a specific address and port
server_address = ('192.168.1.53', 7777)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)
print('Waiting for a connection...')

while True:
    # Wait for a connection
    conn, client_address = sock.accept()
    print('Connection from', client_address)

    try:
        # Send the server public key to the client
        server_public_key = key.publickey().export_key()
        conn.sendall(server_public_key)

        # Receive the encrypted symmetric key from the client
        encrypted_key = conn.recv(256)
        cipher = PKCS1_OAEP.new(key)
        symmetric_key = cipher.decrypt(encrypted_key)

        # Receive the encrypted file from the client
        encrypted_file = conn.recv(2048)
        decipher = PKCS1_OAEP.new(key=symmetric_key)
        decrypted_file = decipher.decrypt(encrypted_file)
        # Write the decrypted file to disk
        with open('tuto33a_file', 'wb') as f:
            f.write(decrypted_file)
        print('File received and decrypted successfully.')

    finally:
        # Clean up the connection
        conn.close()

我只是不知道我得到这个错误的原因,它看起来像对象decrypt()是期待不是它收到的一个(encrypted_file).我也得到了这个职位的解释开始:RSA decryption using "pyca/cryptography" getting error "AttributeError: 'bytes' object has no attribute 'decrypt'"但我仍然找不到如何修复它:(谁能指出我在代码中的错误,并解释我做错了什么,好吗?

iyfamqjs

iyfamqjs1#

您看到的错误是由于Crypto包的PKCS1_OAEP模块中的属性错误,该模块用于在服务器端解密加密文件。
错误消息指示用于解密文件的“_key”对象是bytes对象,该对象没有“n”属性。“n”属性指用于加密的RSA密钥的模数,并且是解密所必需的。
此错误表明用于解密的密钥对象未正确初始化,或者对象类型不正确。可能是密钥对象在传递到服务器之前未正确编码,或者序列化或反序列化不正确。
若要解决此问题,您可能需要验证用于解密的密钥对象的类型是否正确,以及该密钥对象是否已正确初始化和编码。您可能还需要验证密钥对象是否已正确地从客户端传递到服务器,以及任何序列化或反序列化步骤是否已正确执行。
此外,如果Crypto包继续出现问题,您可能需要考虑使用其他加密库或方法。

相关问题