提问人:Clashers Lab 提问时间:11/16/2023 最后编辑:JonasClashers Lab 更新时间:11/20/2023 访问量:42
我正在尝试加密发送数据,使用套接字接收数据,然后解密它
i am trying to encrypt send the data over with socket receving it and then decrypting it
问:
我正在尝试加密发送数据,使用套接字接收数据,然后解密数据,但我收到此错误 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)
我希望它能够从一个文件发送加密部分,并从另一个脚本中解密它
答: 暂无答案
评论
send()
len(msg)
Server.recv(expected_length, socket.MSG_WAITALL)