读取文件的二进制文件,加密字符串,通过套接字发送并解密,在Python 3中将二进制文件写入文件?

时间:2018-10-21 07:21:33

标签: python-3.x sockets encryption binary-data

Client.py

from socket import *
from threading import Thread

from Crypto.Cipher import AES

# f is for opening the file and reading the binary data
# that I later want to send to another client
f = open('book.epub', 'rb')

# f2 is to see if there is anything wrong with my method of writing
# So I open the file f2 in 'wb' mode and later encrypt everything I read
# from f and then decrypt it so that I can see if the encryption /
# decryption method changes any of the bytes
f2 = open('example.epub', 'wb')


# Used to encrypt messages
def encrypting(message):
    obj = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
    ciphertext = obj.encrypt(message)
    return ciphertext


# Used to decrypt messages
def decrypting(ciphertext):
    obj2 = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
    message = obj2.decrypt(ciphertext)
    return message


def main():
    s = socket()
    s.connect(('localhost', 15000))
    # Now I'm connected to the localhost at port 15000

    # Reads 512 charactes of file "f" and
    # Make data equal those characters
    data = f.read(512)

    # While data that I'm reading is not empty, write data to file f2
    # and encrypt it so it can be securely sent
    # When I've read the whole file "f", the data variable becomes empty
    # And the while loop ends
    while data:
        data = encrypting(data)
        data = decrypting(data)

        # I do an encryption and decryption to make sure
        # That there is nothing wrong with the process.
        # If I can't open the file "f2" after the program is done
        # then I can't possibly open the file I just send to another
        # computer, so I just double check
        print('Writing data: ' + str(data))
        f2.write(data)

        # Encrypts data after making sure that the data
        # Is not mangled during encryption / decryption 
        data = encrypting(data)
        # Sending encrypted data to server
        s.send(data)

        # Reading the next 512 characters of file f
        data = f.read(512)

# Closing all the open streams
s.close()
f.close()
f2.close()


if __name__ == '__main__':
    main()

Server.py

from socket import *
from threading import Thread

from Crypto.Cipher import AES

# Opening file "fi", which is the file I will be writing
# All the binary data I've received from the client
fi = open('trash.epub', 'wb')


# Used to encrypt the data
def encrypting(message):
    obj = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
    ciphertext = obj.encrypt(message)
    return ciphertext


# Used to decrypt the received data
def decrypting(ciphertext):
    obj2 = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
    message = obj2.decrypt(ciphertext)
    return message


def client_handler():
    conn, addr = s.accept()
    print(str(addr) + ' connected!')

    # Continues until data is empty
    while True:
        # Listens and receives a whopping 4096 
        data = conn.recv(4096)

        # Breaks is data send is empty
        # Client send empty data when reading past the file lengt
        # Hence the file reading and sending is complete
        if data == b'':
            conn.close()
            break

        # Decrypting the encrypted data
        data = decrypting(data)

        # Printing the data the program is about to write
        # As well as it's type, just for debug purposes
        print('Writing: ' + str(data) + '\n\n')
        print('Type: ' + str(type(data)))

        # Writes the decrypted binary data received from the client
        fi.write(data)

    # Closes streams
    fi.close()
    s.close()


HOST = "localhost"
PORT = 15000


# Initializing the server on
# the address: localhost
# and the port: 15000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)

print("Server is runnig")

# Executes the client_handler(), which listens to incoming data
client_handler()

当我尝试使用mupdf(我在Arch Linux上)打开trash.epub时,出现错误消息:

error: zlib inflate error: invalid distance too far back

book.epub和example.epub这两个文件都可以正常工作,但是rash.epub只是...垃圾。 “ trash.epub”与其他两个文件大小相同。当我输入

nano book.epub

当我输入时发现SAME数据,相同的行数,相同的字符数     纳米垃圾桶 但是,我似乎无法打开rash.epub。我可以毫无问题地打开example.epub,但是rash.epub可能有些问题。有什么想法吗?

0 个答案:

没有答案