Python Forum
need help one time pad encryption implementation !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help one time pad encryption implementation !
#1
Hello guys ! i want to implemante a onetimepad encryption to my socket chat program..but i dont know how !
this is the onetimepad library : https://pypi.org/project/onetimepad/

Client code :
#!/usr/bin/env python3

import select
import socket
import sys

# usage: ./client.py [PORT] [HOST]

if __name__ == "__main__":

	if len(sys.argv) == 1:
		HOST = ("localhost", 10000)
	elif len(sys.argv) == 2:
		HOST = ("localhost", int(sys.argv[1]))
	else:
		HOST = (sys.argv[2], int(sys.argv[1]))

	main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

	try:
		main_socket.connect(HOST)
		sys.stdout.write("Connected to " + HOST[0] + ":" + str(HOST[1]) + '\n')
		sys.stdout.flush()
	except:
		sys.stdout.write("Could not connect to " + HOST[0] + ":" + str(HOST[1]) + '\n')
		sys.stdout.flush()
		exit(2)

	while True:
		read_buffers = [sys.stdin, main_socket]
		try:
			read_list, write_list, error_list = select.select(read_buffers, [], [])

			for sock in read_list:
				if sock == main_socket:
					data = sock.recv(4096)
					if data:
						data = data.decode()
						sys.stdout.write(data)
						sys.stdout.flush()
					else:
						print("Disconnected from server!")
						exit(2)
				else:
					msg = sys.stdin.readline()
					sys.stdout.write("You> " + msg)
					sys.stdout.flush()
					main_socket.send(msg.encode())

		except KeyboardInterrupt:
			print("Disconnected from server!")
			exit(1)
Server code :

#!/usr/bin/env python3

import socketserver
import sys
import threading

# usage: ./server.py [PORT] [HOST]

CLIENTS = []


class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
	pass


class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

	# Class is instantiated once per connection to the server

	def handle(self):
		CLIENTS.append(self.request)
		welcomeMsg = self.client_address[0] + ":" + str(self.client_address[1]) + " joined." + '\n'
		sys.stdout.write(welcomeMsg)
		sys.stdout.flush()
		for cli in CLIENTS:
			if cli is not self.request:
				cli.sendall(welcomeMsg.encode())
		while True:
			data = self.request.recv(4096)
			if data:
				data = data.decode()
				sendMsg = self.client_address[0] + ":" + str(self.client_address[1]) + "> " + data
				sys.stdout.write(sendMsg)
				sys.stdout.flush()
				for cli in CLIENTS:
					if cli is not self.request:
						cli.sendall(sendMsg.encode())
			else:
				sendMsg = self.client_address[0] + ":" + str(self.client_address[1]) + " left." + '\n'
				sys.stdout.write(sendMsg)
				sys.stdout.flush()
				CLIENTS.remove(self.request)
				for cli in CLIENTS:
					cli.sendall(sendMsg.encode())
				break


if __name__ == "__main__":

	if len(sys.argv) == 1:
		HOST = ("localhost", 10000)
	elif len(sys.argv) == 2:
		HOST = ("localhost", int(sys.argv[1]))
	else:
		HOST = (sys.argv[2], int(sys.argv[1]))

	server = ThreadedTCPServer(HOST, ThreadedTCPRequestHandler)
	server.daemon_threads = True

	server_thread = threading.Thread(target=server.serve_forever)

	# Exit the server thread when the main thread terminates
	server_thread.daemon = True
	server_thread.start()

	sys.stdout.write("Server is up." + '\n')
	sys.stdout.flush()

	# Main execution will push
	while True:
		try:
			msg = sys.stdin.readline()
			msg = "Server> " + msg
			sys.stdout.write(msg)
			sys.stdout.flush()
			for client in CLIENTS:
				client.sendall(msg.encode())

		except KeyboardInterrupt:
			break

	server.shutdown()
	server.server_close()
	sys.stdout.write("Server is closed." + '\n')
	sys.stdout.flush()
Reply
#2
note : i think only the client code will be modified . i want only clients can decrypt onetimepad .
and the server will log only ciphred messages !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encryption and decryption with python ibrahim 1 612 May-16-2020, 03:14 PM
Last Post: Larz60+
  File encryption itzik 5 919 Nov-05-2019, 12:29 PM
Last Post: Gribouillis
  Vernam encryption method for files JohnCTX 1 818 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  Regarding encryption and decryption naressh1994 1 1,062 Jan-25-2019, 07:26 AM
Last Post: buran
  AES encryption - does not match between arduino and python 3 crypto guillaume55 0 2,437 Sep-23-2018, 11:14 AM
Last Post: guillaume55
  Python function that uses a word as the encryption key, rather than an integer wak_stephanie 4 2,408 Aug-31-2018, 12:16 PM
Last Post: perfringo
  lab experiment / encryption pythan 0 1,348 Jun-09-2018, 07:19 PM
Last Post: pythan
  Errors in my encryption algorithm SlimeBOOS 1 1,417 Dec-16-2017, 06:23 PM
Last Post: SlimeBOOS
  Attempting to port XTea Encryption from C to Python sonic1015 1 1,962 Jun-06-2017, 07:12 PM
Last Post: sonic1015
  Testing homemade encryption program in python LEJONKINGEN 1 2,042 Mar-04-2017, 04:09 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020