Created
April 24, 2019 14:27
-
-
Save ignertic/9b800d584fca4e712cd4d978c8d40f7b to your computer and use it in GitHub Desktop.
Encrypted Python TCP Socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import sys | |
from tcp import * | |
def handler(data): | |
print(data) | |
return input(">> ") | |
if __name__ == '__main__': | |
host = 'localhost' | |
port = 1189 | |
delimit = '.' | |
key = input("key>> ") | |
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
connection.connect((host, port)) | |
comm(connection, delimit, key, handler, host) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import pickle | |
from tcp import * | |
def pwrite(data, pname): | |
pfile = open(pname, 'wb') | |
pickle.dump(data, pfile) | |
pfile.close() | |
def pread(pname): | |
pfile = open(pname, 'rb') | |
tmp = pickle.load(pfile) | |
pfile.close() | |
return tmp | |
def welcome(connection): | |
out_data = 'Connection established (' + now() + ')!' | |
out_data = paste(out_data, delimit, 1) | |
tkey = paste(key, delimit, 2) | |
out_data = encrypt(out_data, tkey, delimit) | |
out_data = paste(out_data, delimit, 0) | |
send(connection, out_data) | |
def handler(data): | |
bulletin = 'bulletin' | |
data = data.split(' ') | |
if data [0] == 'read': | |
try: | |
return pread(bulletin) | |
except Exception as e: | |
print(str(e)) | |
return "Read unsucessful!" | |
if data[0] == 'write': | |
tmp = ' '.join(data[1:len(data)]) | |
try: | |
tmp += ('\n' + pread(bulletin)) | |
pwrite(tmp, bulletin) | |
return 'Write successful!' | |
except Exception as e: | |
pwrite(tmp, bulletin) | |
return "Write sucessful! (created new file)." | |
if data[0] == 'help': | |
return 'help, read, write [message]' | |
return 'Error. Try again.' | |
if __name__ == '__main__': | |
port = 1189 | |
delimit = '.' | |
key = '01189998819991197253' | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.bind(('', port)) | |
#sock.bind((socket.gethostname(), port)) | |
sock.listen(5) | |
while True: | |
connection, address = sock.accept() | |
print('Incoming connection (%s): %s' % (now(), address[0])) | |
welcome(connection) | |
thread(connection, delimit, key, handler, address[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Cipher import AES | |
from math import ceil | |
import base64 | |
import time | |
import sys | |
import _thread | |
import select | |
def now(): | |
return time.ctime(time.time()) | |
def thread(connection, delimit, key, handler, address): | |
_thread.start_new_thread(comm, (connection, delimit, key, handler, address)) | |
def comm(connection, delimit, key, handler, address): | |
while True: | |
time.sleep(1) | |
in_data = connection.recv(1024) | |
in_data = cut(delimit, in_data) | |
tkey = paste(key, delimit, 2) | |
in_data = decrypt(in_data, tkey, delimit) | |
in_data = cut(delimit, in_data) | |
if in_data == 'exit': | |
time.sleep(1) | |
print('Terminating connection (%s): %s' % (now(), address)) | |
connection.close() | |
break | |
out_data_plain = handler(in_data) | |
out_data = paste(out_data_plain, delimit, 1) | |
tkey = paste(key, delimit, 2) | |
out_data = encrypt(out_data, tkey, delimit) | |
out_data = paste(out_data, delimit, 0) | |
send(connection, out_data) | |
if out_data_plain == 'exit': | |
time.sleep(1) | |
print('Terminating connection (%s): %s' % (now(), address)) | |
connection.close() | |
break | |
def cut(delimit, data): | |
while data[-1] == delimit: | |
data = data[0:-1] | |
return data | |
def paste(data, delimit, mode): | |
if mode == 0: | |
while len(data) % 1024: | |
data += delimit.encode() | |
elif mode == 1: | |
while len(data) % 16: | |
data += delimit | |
elif mode == 2: | |
while not len(data) in [16, 24, 32]: | |
data += delimit | |
return data | |
def send(connection, data): | |
for i in range(int(len(data)/1024)): | |
tmp = data[i*1024:(i+1)*1024] | |
connection.send(tmp) | |
def encrypt(data, key, delimit): | |
if len(key) > 32: | |
print("Key too long! Bye bye.") | |
sys.exit() | |
data = data.encode() | |
cipher = AES.new(key, AES.MODE_ECB) | |
return base64.b64encode(cipher.encrypt(data)) | |
def decrypt(data, key, delimit): | |
if len(key) > 32: | |
print("Key too long! Bye bye.") | |
sys.exit() | |
cipher = AES.new(key, AES.MODE_ECB) | |
return cipher.decrypt(base64.b64decode(data)).decode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment