通过套接字发送加密数据和解密不起作用 [英] Sending Encrypted Data Through Socket And Decrypting Doesn't Work
问题描述
我正在创建一个简单的加密软件.我目前遇到的问题是通过套接字发送加密的 aes 文件数据不起作用.在接收端,应该写入的文件是空的.我已经浏览了我的代码一段时间,但看不到解决它.
I am creating a simple encryption software. The problem I currently have is that sending encrypted aes file data through a socket doesn't work. At the receiving end, the file that should be written to is empty. I have looked through my code for a good while and can't see to solve it.
我做了一个没有网络的版本.我已经能够在不同版本上发送最大 8 KB 的小文件
I have made a version without networking. I have been able to send a small file up to 8 KB on a different version
我的程序是基于函数的,所以程序从主菜单分支到其他菜单和函数.由于有一点跳跃,最好显示所有代码.https://github.com/BaconBombz/Dencryptor/blob/Version-2.0/Dencryptor.py
My Program Is Function Based So The Program Branches Off From The Main Menu To Other Menues And Functions. Since There is A Bit Of Jumping, It Would Be Best To Show All The Code. https://github.com/BaconBombz/Dencryptor/blob/Version-2.0/Dencryptor.py
套接字连接,并且发送所有需要的数据.然后,文件被 AES 加密并通过套接字发送.接收端将加密数据写入文件并解密.程序会说文件已发送,但在接收端,程序吐出一个结构错误,因为应该包含加密数据的文件是空的.
The socket connects, and all required data is sent. Then, the file is AES encrypted and sent through the socket. The Receiving end writes encrypted data to a file and decrypts it. The program will say the file was sent but on the recieving end, the program spits out a struct error because the file that should have the encrypted data is empty.
推荐答案
代码太不极简 所以这里是极简下载未加密文件的示例.此外,TCP 是一种流协议,使用睡眠来分离数据是不正确的.而是为字节流定义一个协议.这是我的示例的协议:
The code is too non-minimal so here's a minimal example of downloading an unencrypted file. Also, TCP is a streaming protocol and using sleeps to separate your data is incorrect. Define a protocol for the byte stream instead. This is the protocol of my example:
- 打开连接.
- 发送后跟换行符的 UTF-8 编码文件名.
- 以十进制格式发送编码文件大小,后跟换行符.
- 发送文件字节.
- 关闭连接.
注意 Python 3 代码.Python 2 对我来说已经死了.支持将于明年结束,请升级!
Note Python 3 code. Python 2 is dead to me. Support ends next year for it so upgrade!
server.py
from socket import *
import os
CHUNKSIZE = 1_000_000
# Make a directory for the received files.
os.makedirs('Downloads',exist_ok=True)
sock = socket()
sock.bind(('',5000))
sock.listen(1)
with sock:
while True:
client,addr = sock.accept()
# Use a socket.makefile() object to treat the socket as a file.
# Then, readline() can be used to read the newline-terminated metadata.
with client, client.makefile('rb') as clientfile:
filename = clientfile.readline().strip().decode()
length = int(clientfile.readline())
print(f'Downloading {filename}:{length}...')
path = os.path.join('Downloads',filename)
# Read the data in chunks so it can handle large files.
with open(path,'wb') as f:
while length:
chunk = min(length,CHUNKSIZE)
data = clientfile.read(chunk)
if not data: break # socket closed
f.write(data)
length -= len(data)
if length != 0:
print('Invalid download.')
else:
print('Done.')
client.py
from socket import *
import os
CHUNKSIZE = 1_000_000
filename = input('File to upload: ')
sock = socket()
sock.connect(('localhost',5000))
with sock,open(filename,'rb') as f:
sock.sendall(filename.encode() + b'
')
sock.sendall(f'{os.path.getsize(filename)}'.encode() + b'
')
# Send the file in chunks so large files can be handled.
while True:
data = f.read(CHUNKSIZE)
if not data: break
sock.sendall(data)
这篇关于通过套接字发送加密数据和解密不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!