I am trying to send an encrypted file over TCP sockets in python, I don't want to have to encrypt the message, save it in %TEMP% and then send it (it could fill up hard drive space).
I am following this code I found online at: https://gist.github.com/giefko/2fa22e01ff98e72a5be2
Here is my server code:
from random import choice
import socket, os, threading, json
from cryptography.fernet import Fernet
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPWRSTUVWXYZ1234567890!##$%^&*()"
#read the key or generate
key = b""
if os.path.exists("client.key"):
with open("client.key", "rb") as f:
key = f.read()
else:
with open("client.key", "wb") as f:
key = Fernet.generate_key()
f.write(key)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 34467
host = "0.0.0.0"
s.bind((host, port))
print(f"LISTENING ON {host}:{port}")
s.listen(100)
def new_salt():
salt = ""
for x in range(15):
salt += choice(chars)
return salt
def handle_client(conn, addr):
encryption = False
def send_raw(content_type, Bytes_, salt=new_salt()):
seperator = "<|SEPERATE|>"
to_send = content_type + seperator + Bytes_.decode() + seperator + salt
to_send = to_send.encode()
if encryption:
to_send = Fernet(key).encrypt(to_send)
conn.send(to_send)
def recv_raw(BufferSize):
seperator = "<|SEPERATE|>".encode()
data = b""
while True:
data = conn.recv(BufferSize)
if data: break
if encryption:
data = Fernet(key).decrypt(data)
splitted = data.decode().split(seperator.decode())
content_type = splitted[0]
Bytes_ = splitted[1].encode()
salt = splitted[2]
return {"content_type": content_type, "bytes": Bytes_}
print("NEW CLIENT AT IP: " + str(addr[0]))
print("EXTANGING KEY")
send_raw("KEY", key)
client_key = recv_raw(1024)["bytes"]
if key == client_key:
print("KEY EXTANGE VERIFIED")
else:
print("UNABLE TO VERIFY, CLIENT MAY EXPERIENCE ISSUES")
print(key)
print(client_key)
encryption = True
print("GRAPPING SYSTEM INFO...")
sys_info_request = recv_raw(1024)
print("RECIVED, DECODING...")
sys_info = json.loads(sys_info_request["bytes"].decode())
print("BASIC INFO:")
print("Platoform: " + sys_info["platform"])
print("Architecture: " + str(sys_info["architecture"]))
print("Username: " + sys_info["username"])
if os.path.exists("autorun.txt"):
with open("autorun.txt", "r") as f:
print("FOUND AUTORUN, EXECUTING COMMANDS")
for line in f.readlines():
print("> " + line)
send_raw("command", line.encode())
output = recv_raw(1024)
print(output["bytes"].decode())
current_dir = sys_info["current_dir"]
while True:
try:
cmd = input(current_dir + "> " + sys_info["username"] + " $ ")
if cmd == "abort":
send_raw("abort", "".encode())
conn.close()
print("SAFE")
break
if cmd == "send_file":
# CODE GOES HERE
send_raw("command", cmd.encode())
output = recv_raw(1024)["bytes"].decode()
print(output)
except:
print("UNEXCPECTED ERROR")
while True:
conn, addr = s.accept()
threading.Thread(target=handle_client, args=(conn,addr,)).start()
I haven't found anything online that will work in my senario.
Okay, so you want to open a file, encrypt it and send it and avoid writing a tempfile to the hard disc, right? This works (taken from the example server code you linked):
while True:
conn, addr = s.accept() # Establish connection with client.
print('Got connection from', addr)
data = conn.recv(1024)
print('Server received', repr(data))
from cryptography.fernet import Fernet
key = Fernet.generate_key()
ff = Fernet(key)
filename='crs.py' #In the same folder or path is this file running must the file you want to tranfser to be
f = open(filename,'rb')
l = f.read(1024)
while (l):
enc = ff.encrypt(l)
conn.send(enc)
print('Sent ',repr(enc))
l = f.read(1024)
f.close()
print('Done sending')
conn.send(b'Thank you for connecting')
conn.close()
So, I am just opening the file, reading it 1024 bytes a time, encrypting it and then sending it along .. Does that answer your question?
Related
I am using below provided code to retrive some data with the socket. Issue with this code, it prints all the results till it breaks eventhough I only care for the received line right before it breaks, the second last in other words. So, I need some help to understand how that can be achieved.
import socket
import time
socket.setdefaulttimeout(10)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("192.168.50.102", 2102))
curIndex = "0"
while True:
sending_data = 'get,trx,'+curIndex
#print sending_data
client.send(sending_data)
data = client.recv(128)
print data.encode('UTF-8')
if data == "trx,notfound": break
spdata = data.split(",")
#print spdata[2] + 'kg' #Prints weight + kg
if len(spdata) >= 3:
curIndex = spdata[1]
time.sleep(0.5)
client.close()
Actual output
trx,2,1.250,0.000,19-07-11 14:08:01
trx,3,0.500,0.000,19-07-11 14:19:24
trx,4,0.500,0.000,19-07-11 15:04:37
trx,5,0.250,0.000,19-07-11 15:05:31
trx,6,0.177,0.000,19-07-11 21:06:59
trx,7,0.108,0.000,19-07-12 14:54:00
trx,8,0.106,0.000,19-07-16 17:51:06
trx,9,0.106,0.000,19-07-16 17:54:24
trx,10,0.106,0.000,19-07-18 14:31:49
trx,11,0.171,0.000,19-07-18 14:51:31
trx,notfound
Desired output
trx,11,0.171,0.000,19-07-18 14:51:31
Do not print it : save it.
Replace :
print data.encode('UTF-8')
if data == "trx,notfound": break
By
if data == "trx,notfound": break
last_data = data.encode('UTF-8')
Then, at the end (like after client.close()) you can print last_data
try
data_b = client.recv(1024)
while data_b:
data_b = client_socket.recv(1024)
data += data_b
instead of
data = client.recv(128)
example
import socket
def main():
port = 'Your port'
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", int(port)))
server_socket.listen(5)
print('TCPServer Waiting for client on port '+port+'\n')
while True:
print('Listening ...', end='\r')
client, address = server_socket.accept()
print("Connection from ", address[0])
data = None
while True:
data_b = client.recv(1024)
data = data_b
if data_b:
print("Receiving a file...")
while data_b:
data_b = client_socket.recv(1024)
data += data_b
else:
break
print(data.decode())
client.close()
if __name__ == '__main__':
main()
When I start the program to share files to client it received it but when i request for another file download it failed with this error.
Now i keep getting this error from the client
socket1.send(bytes('0', 'UTF-8'))
BrokenPipeError: [Errno 32] Broken pipe
line 46 client.py
I tried breaking out of the server's filedownload loop but still not working.
server
#! /usr/bin/env python3
import socket
import sys
import os
import hashlib
import time
HOST = 127.0.0.1
PORT = 5000
c = 0 #used to count cycles
bufsize = 4096
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print('Server Created')
except OSError as e:
print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
try:
s.bind((HOST, PORT))
except OSError as e:
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print('Socket bind complete')
s.listen(1)
print('Server now listening')
while 1:
conn, addr = s.accept()
print('Connected with ' + addr[0] + ':' + str(addr[1]))
reqCommand = conn.recv(1024).decode("utf-8", errors='ignore')
print('Client> %s' % reqCommand)
string = reqCommand.split(' ', 1)
if reqCommand == 'quit':
break
elif reqCommand == 'lls':
toSend = ""
path = os.getcwd()
dirs = os.listdir(path)
for f in dirs:
toSend = toSend + f + ' '
conn.send(toSend.encode('utf-8'))
# print path
else:
string = reqCommand.split(' ', 1) # in case of 'put' and 'get' method
if len(string) > 1:
reqFile = string[1]
if string[0] == 'FileDownload':
with open(reqFile, 'rb') as file_to_send1:
# get the entire filesize, which sets the read sector to EOF
file_size = len(file_to_send1.read())
# reset the read file sector to the start of the file
file_to_send1.seek(0)
# take filesize and write it to a temp file
with open('temp01',mode='w', encoding='UTF-8') as file_to_send2:
file_to_send2.write(str(file_size))
# pass the file size over to client in a small info chunk
with open('temp01', 'rb') as file_to_send3:
conn.send(file_to_send3.read(1024))
#send the total file size off the client
while (c*bufsize) < file_size:
send_data = file_to_send1.read(bufsize)
conn.send(send_data)
c += 1
# get bool (0 is bad | 1 is good) from client
chunk_write_flag = int(conn.recv(1024))
while chunk_write_flag != 1: #while whole data was not sent..retry until successful
conn.send(send_data)
#get status from client after a retry
chunk_write_flag = int(conn.recv(1024))
# used on the last chunk of the file xfer
# if the file.read() is less than buffer size do last tasks
if (file_size - (c*bufsize)) < bufsize:
send_data = file_to_send1.read(bufsize)
conn.send(send_data)
file_to_send1.close()
break
#for data in file_to_send:
#conn.sendall(data)
print('Send Successful')
conn.close()
s.close()
client
#! /usr/bin/env python3
import socket
import sys
import os
import hashlib
import time
HOST = 127.0.0.1
PORT = 5000
c = 0
bufsize = 4096
def get(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName.encode("utf-8"))
string = commandName.split(' ', 1)
inputFile = string[1]
c = 0
# before starting to write new file, get file size
file_size = int(socket1.recv(1024)) # from file_to_send3
# print (file_size)
# set byte buffer size
bufsize = 4096
# start writing at the beginning and use following variable to track
write_sectors = 0
# this only opens the file, the while loop controls when to close
with open(inputFile, 'wb+') as file_to_write:
# while written bytes to out is less than file_size
while write_sectors < file_size:
# write the BUFSIZE while the write_sector is less than file_size
file_to_write.write(socket1.recv(bufsize))
c += 1
with open(inputFile, 'rb') as verify:
write_check = (len(verify.read()) / c)
verify.seek(0) # read cycle moves seek location, reset it
while write_check != bufsize:
# where the original write started, to send back to server
if c > 1: file_to_write.seek((c-1) * bufsize)
if c == 1: file_to_write.seek(0)
# send to server that the write was not successful
socket1.send(bytes('0', 'UTF-8'))
file_to_write.write(socket1.recv(bufsize))
write_check = int(len(verify.read()) /c )
# if last packet, smaller than bufsize
socket1.send(bytes('1', 'UTF-8')) #send SUCCESS back to server
if (file_size - (write_check * c)) < bufsize:
#file_to_write.write(socket1.recv(bufsize))
verify.close()
#file_to_write.close()
file_size = 0
write_sectors += bufsize # successful write, move 'while' check
# add the written sectors by the bufsize.
# example if filesize in bytes is 4096 you need to track how much
# was written to figure out where the EOF is
file_to_write.write(socket1.recv(bufsize)) # write the last chunk missed by while loop
#data = socket1.recv(4096).decode("utf-8", errors="ignore")
#if not data: break
#break
# print data
#file_to_write.write(bytes(data.encode()))
#file_to_write.close()
print('Download Successful')
socket1.close()
return
def serverList(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName.encode('utf-8'))
fileStr = socket1.recv(1024)
fileList = fileStr.decode('utf-8').split(' ')
for f in fileList:
print(f)
socket1.close()
return
msg = input('Enter your name: ')
while 1:
print("\n")
print('"FileDownload [filename]" to download the file from the server ')
print('"lls" to list all files in the server')
sys.stdout.write('%s> ' % msg)
inputCommand = sys.stdin.readline().strip()
if inputCommand == 'lls':
serverList('lls')
else:
string = inputCommand.split(' ', 1)
if string[0] == 'FileDownload':
Please can anyone help me,i don't know to fix it. I'll appreciate any help.
Thanks
Server.py
class BufferedReceiver():
def __init__(self, sock):
self.sock = sock
self.buffer = ""
self.bufferPos = 0
def _fetch(self):
while self.bufferPos >= len(self.buffer):
self.buffer = self.sock.recv(1024)
# print(self.buffer)
self.bufferPos = 0
def take(self, amount):
result = bytearray()
while(len(result) < amount):
# Fetch new data if necessary
self._fetch()
result.append(self.buffer[self.bufferPos])
self.bufferPos += 1
return bytes(result)
def take_until(self, ch):
result = bytearray()
while True:
# Fetch new data if necessary
self._fetch()
nextByte = self.buffer[self.bufferPos]
self.bufferPos += 1
result.append(nextByte)
if bytes([nextByte]) == ch:
break
return bytes(result)
Then, I simplified your server send routine after the else::
string = reqCommand.split(' ', 1) # in case of 'put' and 'get' method
if len(string) > 1:
reqFile = string[1]
if string[0] == 'FileDownload':
with open(reqFile, 'rb') as file_to_send1:
# get the entire filesize, which sets the read sector to EOF
file_size = len(file_to_send1.read())
# reset the read file sector to the start of the file
file_to_send1.seek(0)
# pass the file size over to client in a small info chunk
print('Filesize:', file_size)
conn.send((str(file_size)+'\n').encode())
#send the total file size off the client
c = 0
while (c*bufsize) < file_size:
send_data = file_to_send1.read(bufsize)
conn.send(send_data)
c += 1
print('Send Successful')
Client.py
def get(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName.encode("utf-8"))
receiver = BufferedReceiver(socket1)
string = commandName.split(' ', 1)
inputFile = string[1]
# before starting to write new file, get file size
file_size = int(receiver.take_until(b'\n').decode().strip()) # from file_to_send3
print ('Filesize:', file_size)
# set byte buffer size
bufsize = 4096
# this only opens the file, the while loop controls when to close
with open(inputFile, 'wb+') as file_to_write:
# while written bytes to out is less than file_size
c = 0
while True:
# Compute how much bytes we have left to receive
bytes_left = file_size - bufsize * c
# If we are almost done, do the final iteration
if bytes_left <= bufsize:
file_to_write.write(receiver.take(bytes_left))
break
# Otherwise, just continue receiving
file_to_write.write(receiver.take(bufsize))
c += 1
#TODO open file again, verify.
# Generate MD5 on server while sending. Then, send MD5 to client.
# Open finished file in client again and compare MD5s
print('Download Successful')
socket1.close()
return
I am trying to implement a peer to peer file transfer protocol and i came across this code but when I ported it to python 3.6 I got this error "type error a bytes-like object is required not 'str'". Please can somebody help me out i am new to this.
server.py
! /usr/bin/python3.6
import subprocess
import socket
import sys
import os
import hashlib
HOST = 'localhost'
PORT = 8000
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Server Created')
except OSError as e:
print('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
try:
s.bind((HOST, PORT))
except OSError as e:
print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print('Socket bind complete')
s.listen(1)
print('Server now listening')
while (1):
conn, addr = s.accept()
print('Connected with ' + addr[0] + ':' + str(addr[1]))
reqCommand = conn.recv(1024)
print('Client> %s' %(reqCommand))
string = reqCommand.split(' ')
if (reqCommand == 'quit'):
break
elif (reqCommand == 'lls'):
toSend=""
path = os.getcwd()
dirs=os.listdir(path)
for f in dirs:
toSend=toSend+f+' '
conn.send(toSend)
#print path
elif (string[1]== 'shortlist'):
path = os.getcwd()
command = 'find '+path+ ' -type f -newermt '+string[2]+' '+string[3]+ ' ! -newermt '+string[4]+' '+string[5]
var = commands.getstatusoutput(command)
var1 = var[1]
var=var1.split('\n')
rslt = ""
for i in var:
comm = "ls -l "+i+" | awk '{print $9, $5, $6, $7, $8}'"
tup=commands.getstatusoutput(comm)
tup1=tup[1]
str=tup1.split(' ')
str1=str[0]
str2=str1.split('/')
rslt=rslt+str2[-1]+' '+str[1]+' '+str[2]+' '+str[3]+' '+str[4]+'\n'
conn.send(rslt)
elif (string[1]=='longlist'):
path = os.getcwd()
var= commands.getstatusoutput("ls -l "+path+" | awk '{print $9, $5, $6, $7, $8}'")
var1 = ""
var1= var1+''+var[1]
conn.send(var1)
elif (string[0] == 'FileHash'):
if(string[1]== 'verify'):
BLOCKSIZE = 65536
hasher = hashlib.sha1()
with open(string[2], 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
conn.send(hasher.hexdigest())
print('Hash Successful')
elif (string[1] == 'checkall'):
BLOCKSIZE = 65536
hasher = hashlib.sha1()
path = os.getcwd()
dirs=os.listdir(path)
for f in dirs:
conn.send(f)
with open(f, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
conn.send(hasher.hexdigest())
print('Hash Successful')
else:
string = reqCommand.split(' ') #in case of 'put' and 'get' method
if(len(string) > 1):
reqFile = string[1]
if (string[0] == 'FileUpload'):
file_to_write = open(reqFile,'wb')
si = string[2:]
for p in si:
p = p + " "
print("User" + p)
file_to_write.write(p)
while True:
data = conn.recv(1024)
print("User" + data)
if not data:
break
file_to_write.write(data)
file_to_write.close()
print('Receive Successful')
elif (string[0] == 'FileDownload'):
with open(reqFile, 'rb') as file_to_send:
for data in file_to_send:
conn.sendall(data)
print('Send Successful')
conn.close()
s.close()
This is the error from the server.
Server Created
Socket bind complete
Server now listening
Connected with 127.0.0.1:37760
Client> b''
Traceback (most recent call last):
File "./server.py", line 36, in <module>
string = reqCommand.split(' ')
TypeError: a bytes-like object is required, not 'str'
This is the client side of my program but having the same problem.
client.py
#! /usr/bin/python3.6
import socket
import sys
import os
import hashlib
HOST = 'localhost' #server name goes in here
PORT = 8000
def put(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
string = commandName.split(' ', 1)
string = commandName.split(' ')
inputFile = string[1]
with open(inputFile, 'rb') as file_to_send:
for data in file_to_send:
socket1.sendall(data)
print("Client users " + data)
socket1.send(data)
print('Upload Successful')
socket1.close()
return
def get(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName(data. 'utf-8'))
string = commandName.split(' ')
inputFile = string[1]
with open(inputFile, 'wb') as file_to_write:
while True:
data = socket1.recv(1024)
if not data:
break
# print data
file_to_write.write(data)
file_to_write.close()
print('Download Successful')
socket1.close()
return
def FileHash(commandName):
string = commandName.split(' ')
if string[1] == 'verify':
verify(commandName)
elif string[1] == 'checkall':
checkall(commandName)
def verify(commandName):
socket1=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
hashValServer=socket1.recv(1024)
string = commandName.split(' ')
BLOCKSIZE = 65536
hasher = hashlib.sha1()
with open(string[2], 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
hashValClient = hasher.hexdigest()
print('hashValServer= %s', (hashValServer))
print('hashValClient= %s', (hashValClient))
if hashValClient == hashValServer:
print('No updates')
else:
print('Update Available')
socket1.close()
return
def checkall(commandName):
socket1=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
string = commandName.split(' ')
BLOCKSIZE = 65536
hasher = hashlib.sha1()
# f=socket1.recv(1024)
while True:
f=socket1.recv(1024)
with open(f, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
hashValClient = hasher.hexdigest()
hashValServer=socket1.recv(1024)
print ('Filename = %s', f)
print('hashValServer= %s', (hashValServer))
print('hashValClient= %s', (hashValClient))
if hashValClient == hashValServer:
print('No updates')
else:
print('Update Available')
if not f:
break
socket1.close()
return
def quit():
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
socket1.close()
return
def IndexGet(commandName):
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
string = commandName.split(' ')
if string[1] == 'shortlist':
socket1.send(commandName)
strng=socket1.recv(1024)
strng=strng.split('\n')
for f in strng:
print(f)
elif (string[1]=='longlist'):
socket1.send(commandName)
path=socket1.recv(1024)
rslt=path.split('\n')
for f in rslt[1:]:
print(f)
socket1.close()
return
def serverList(commandName):
socket1=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.connect((HOST, PORT))
socket1.send(commandName)
fileStr=socket1.recv(1024)
fileList=fileStr.split(' ')
for f in fileList[:-1]:
print(f)
socket1.close()
return
msg = input('Enter your name: ')
while(1):
print("\n")
print("****************")
print('Instruction')
print('"FileUpload [filename]" to send the file the server ')
print('"FileDownload [filename]" to download the file from the server ')
print('"ls" to list all files in this directory')
print('"lls" to list all files in the server')
print('"IndexGet shortlist <starttimestamp> <endtimestamp>" to list the files modified in mentioned timestamp.')
print('"IndexGet longlist" similar to shortlist but with complete file listing')
print('"FileHash verify <filename>" checksum of the modification of the mentioned file.')
print('"quit" to exit')
print("\n")
sys.stdout.write ('%s> ' %msg)
inputCommand = sys.stdin.readline().strip()
if (inputCommand == 'quit'):
quit()
break
elif (inputCommand == 'ls'):
path = os.getcwd()
dirs = os.listdir(path)
for f in dirs:
print(f)
elif (inputCommand == 'lls'):
serverList('lls')
else:
string = inputCommand.split(' ', 1)
if string[0] == 'FileDownload':
get(inputCommand)
elif string[0] == 'FileUpload':
put(inputCommand)
elif string[0] =='IndexGet':
IndexGet(inputCommand)
elif string[0] == 'FileHash':
FileHash(inputCommand)
I expexted it to transfer the file without any error, Please i am new to this can anybody help me out.
This the error from the client
FileUpload closer.mp3
g> Traceback (most recent call last):
File "./client.py", line 193, in <module>
put(inputCommand)
File "./client.py", line 16, in put
socket1.send(commandName)
TypeError: a bytes-like object is required, not 'str'
You're trying to send a string even though a byte array is needed.
in 'socket1.send(commandName)' write socket1.send(commandName.encode('utf-8') instead
same thing serverside.
You should really rethink coding a cryptocurrency if you can't figure out the difference between bytes and strings. Start with an easier project.
I am working on an encryption program with Pycryptodome in Python 3.6 I am trying to encrypt a file and then decrypt it and verify the MAC tag. When I get to verify it, an error is thrown
import os
from Crypto.Cipher import AES
bib Cryptodome imported
aad = b'any thing'
nonce = b'\xde\xe2G\xca\xe8Lq9\xeb\x8b\x84\xe7'
key = b'\xde\xe9\xad\xe9\x14_\x07\x1aq7C\\\xd7\x9c\xae\xfcJ\x1c%\xec\xe6C\xbe\xf0eO\xfaJ1\x08\x0c\xae'
I set the nonce and key as constant just to start things first. then i will use nonce = get_random_bytes(12) unique for every file.
def encrypt(filename):
chunksize = 64 * 1024
outputFile = "(encrypted)" + filename
filesize = str(os.path.getsize(filename))
cipher = AES.new(key, AES.MODE_GCM, nonce)
cipher.update(aad)
with open(filename, 'rb') as infile:
with open(outputFile, 'wb') as outfile:
outfile.write(filesize.encode('utf-8'))
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' '.encode('utf-8') * (16 - (len(chunk) % 16))
ciphertext,sender_tag = cipher.encrypt_and_digest(chunk)
print (sender_tag)
outfile.write(ciphertext)
the decryption part using decrypt_and_verify so no need to worry about which come first the decryption or verification
def decrypt(filename,received_tag):
chunksize = 64 * 1024
outputFile = "(clear)"+ filename
cipher = AES.new(key, AES.MODE_GCM, nonce)
cipher.update(aad)
with open(filename, 'rb') as infile:
with open(outputFile, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
clearmessage = cipher.decrypt_and_verify(chunk, b'received_tag')
outfile.write(clearmessage)
outfile.truncate()
def Main():
choice = input("Would you like to (E)ncrypt or (D)ecrypt?: ")
if choice == 'E':
filename = input("File to encrypt: ")
#password = input("Password: ")
encrypt(filename)
print ("Done.")
elif choice == 'D':
filename = input("File to decrypt: ")
#password = input("Password: ")
received_tag = input("enter received tag:")
decrypt(filename,received_tag)
print ("Done.")
else:
print ("No Option selected, closing...")
if __name__ == '__main__':
Main()
And this is the error:
raise ValueError("MAC check failed")
ValueError: MAC check failed
I don't know where i messed up . by the way i get a tag similar to b'\x1c\xd1\xd8\x1a6\x07\xf3G\x8c_s\x94"*(b'
Update :
i did correct a mistake in the code sender_tag,ciphertext = cipher.encrypt_and_digest(chunk) insted of ciphertext,sender_tag = cipher.encrypt_and_digest(chunk) but still the problem persists
Quite a few of things look dodgy:
You write the file size, but then you don't read it back. Actually, I don't think you should write it at all.
You pad the chunk to the 16 bytes boundary before encrypting it, but GCM does not require that.
None should not be fixed, but random per encryption.
Nonce and MAC tag are typically stored along with the ciphertext (which therefore becomes longer than the plaintext).
By writing chunks independently one from the other you are allowing an attacker to remove, duplicate and reorder chunks in transit.
We need to send a pdf file over a TCP server in a string with the following format:
command number date size file
In the server, we're doing this:
l = f.read()
f.close()
user.sendall(("AQT " + "12345678 " + "16SET2015_12:00:00 " + str(size) + " " + l).encode('utf-8')
Our client looks like this:
quiz = s.recv(buff_size)
quiz_aux = quiz
while(quiz_aux):
quiz_aux = s.recv(buff_size)
quiz += quiz_aux
quiz = quiz.decode('utf-8')
response = quiz.split(" ", 4)
if response[0] == 'AQT':
QID = eval(response[1])
time = response[2]
size = eval(response[3])
file_name = topic + "QF" + "001" + ".pdf"
f = open(file_name, "w")
f.write(response[4])
f.close()
print("received file " + file_name)
We can't seem to get the encoding right, no matter what we try it doesn't work and it also doesn't seem to receive the entire file.
If somebody could help us we'd be truly grateful.
You're most likely opening your file in ASCII instead of BINARY mode. Thats why you do not receive the complete file as you never read the complete file in the first place.
with open(file,'rb') as f:
data = f.read() # reads the complete file.
Here're some flaws in your code:
the decode('utf-8') magic is not required as you'll most likely want to transfer your data byte-by-byte.
calling eval do parse an int is not secure, use int() instead.
storing large files in variables or passing large files to a single send/sendall call is highly inefficient. This is also true for receiving large amounts of data.
the protocol design is flawed. make sure your peer gets a hint about the message size pretty early.
Here's a working example of your code:
client:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import socket
import os
TCP_IP = '127.0.0.1'
TCP_PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
with open(bigfile,'rb') as f:
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(0, os.SEEK_SET)
x = f.read()
s.sendall("AQT " + "12345678 " + "16SET2015_12:00:00 " + str(size) + " " + x)
s.close()
server:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 9999
buff_size = 1024*8
topic = "xyz"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
print "ready"
conn, addr = s.accept()
print 'Connection address:', addr
quiz = conn.recv(buff_size)
quiz_aux = quiz
while(quiz_aux):
quiz_aux = conn.recv(buff_size)
quiz += quiz_aux
response = quiz.split(" ", 4)
print len(response)
if response[0] == 'AQT':
QID = int(response[1])
time = response[2]
size = int(response[3])
file_name = topic + "QF" + "001" + ".txt"
f = open(file_name, "wb")
f.write(response[4])
f.close()
print("received file " + file_name)
conn.close()