我正在尝试加密发送数据,使用套接字接收数据,然后解密它

i am trying to encrypt send the data over with socket receving it and then decrypting it

提问人:Clashers Lab 提问时间:11/16/2023 最后编辑:JonasClashers Lab 更新时间:11/20/2023 访问量:42

问:

我正在尝试加密发送数据,使用套接字接收数据,然后解密数据,但我收到此错误 ValueError:IV 长度不正确(长度必须为 16 字节)

我为我的客户尝试了这个

它采用加密的代码,该代码是一个字节,并尝试通过首先将其作为 str 传递并删除我不需要的符号然后再次使其成为字节来解密它

import socket
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

def recv_decrypt(encrypted_code):
encrypted_text, iv = encrypted_code.split(',')\[0\], encrypted_code.split(',')\[1\]
iv = iv\[2:\]
iv = iv.strip("'")
encrypted_text = encrypted_text\[2:\]
encrypted_text = encrypted_text.strip("'")
encrypted_text = bytes(encrypted_text, 'ascii')
iv = bytes(iv, 'ascii')

    my_key = b'D\xb7\x12t\x85.\xb7\xe7\xea\x08\xed\xd4\xf7\xa3\x9a\x80'
    password = "{hi&wJO@sVDVUq$"
    key = PBKDF2(password, my_key, dkLen=32)

    cipher = AES.new(key, AES.MODE_CBC, iv=iv)
    original = unpad(cipher.decrypt(encrypted_text), AES.block_size)
    print(original)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('localhost', 43785))

while True:
msg = client.recv(1024)
recv_decrypt(encrypted_code=msg.decode())

这是我的服务器代码

这部分对来自用户的消息进行加密,并将其作为字节发送

import socket
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

def send_encrypted(msg, client):
    try:
        my_key = b'D\xb7\x12t\x85.\xb7\xe7\xea\x08\xed\xd4\xf7\xa3\x9a\x80'
        password = "{hi&wJO@sVDVUq$"

        key = PBKDF2(password, my_key, dkLen=32)
        cipher = AES.new(key, AES.MODE_CBC)
        encrypted_data = cipher.encrypt(pad(bytes(msg), AES.block_size))
        iv_data = cipher.iv

        send_data = f'{encrypted_data},{iv_data}'
        client.send(bytes(send_data.encode()))


    except Exception as e:
        print(f"something went wrong in the encrypt section error code: {e}")
        pass

server_ip = "localhost"
connection_port = 43785

Server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Server.bind((server_ip, connection_port))
Server.listen()
client, address = Server.accept()

while True:
    msg = input("MSG: ")
    send_encrypted(msg=msg.encode('ascii'), client=client)

但它一直给我错误

Traceback (most recent call last):
  File "C:\Users\Desktop\GChat\cleen.py", line 30, in <module>
    recv_decrypt(encrypted_code=msg.decode())
  File "C:\Users\Desktop\GChat\cleen.py", line 20, in recv_decrypt
    cipher = AES.new(key, AES.MODE_CBC, iv=iv)
  File "C:\Users\Desktop\GChat\venv\lib\site-packages\Crypto\Cipher\AES.py", line 228, in new
    return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "C:\Users\Desktop\GChat\venv\lib\site-packages\Crypto\Cipher\__init__.py", line 79, in _create_cipher
    return modes[mode](factory, **kwargs)
  File "C:\Users\Desktop\GChat\venv\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 287, in _create_cbc_cipher
    raise ValueError("Incorrect IV length (it must be %d bytes long)" %
ValueError: Incorrect IV length (it must be 16 bytes long)

我希望它能够从一个文件发送加密部分,并从另一个脚本中解密它

python-3.x 套接字网络 编程

评论

1赞 Mark Tolonen 11/16/2023
TCP 不是基于消息的。不要假设一个发送等于一个接收。它是一个字节,您必须检查 send 和 recv 的返回值。
0赞 Clashers Lab 11/16/2023
我该怎么做?
1赞 Barmar 11/17/2023
第一个代码片段中存在缩进问题。请以正确的缩进重新发布。
0赞 Barmar 11/17/2023
@MarkTolonen 除非套接字是非阻塞的,否则无需检查 的返回值 -- 它将全部发送或引发异常。您可以使用 检查收到的长度。send()len(msg)
0赞 Barmar 11/17/2023
但是你需要设计你的协议,这样你就知道每条消息会有多大。一种选择是在消息之前发送长度。然后,服务器读取长度,然后调用以等待该字节数。Server.recv(expected_length, socket.MSG_WAITALL)

答: 暂无答案