Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

python 2.7 - Sending Encrypted Data Through Socket And Decrypting Doesn't Work

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.

I have made a version without networking. I have been able to send a small file up to 8 KB on a different version

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Open the connection.
  2. Send the UTF-8-encoded filename followed by a newline.
  3. Send the encoded file size in decimal followed by a newline.
  4. Send the file bytes.
  5. Close the connection.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...