def testKeyserver(): print("Running keyserver") keyServer = KeyServer keyserverThread = threading.Thread(target=keyServer.main, args=()) keyserverThread.start() print("---KEYSERVER REGISTER TEST---") print("[TEST]Initializing a client1") host = '127.0.0.1' clientsocket_1 = socket.socket() clientsocket_1.connect((host, 2004)) client1PrivateKey = Knapsack.generate_private_key(8) client1PublicKey = Knapsack.create_public_key(client1PrivateKey) print("[TEST]Generated Client1 Public and Private Key:", client1PublicKey, client1PrivateKey) print("[TEST]Sendig keys to keyserver..") message = 'REGISTER#' + str(1) + "#" + str(client1PublicKey) clientsocket_1.send(str.encode(message)) keyServerResponseProcesser(clientsocket_1) print("[TEST]Initializing a client2") clientsocket_2 = socket.socket() clientsocket_2.connect((host, 2004)) client2PrivateKey = Knapsack.generate_private_key(8) client2PublicKey = Knapsack.create_public_key(client2PrivateKey) print("[TEST]Generated Client2 Public and Private Key:", client2PublicKey, client2PrivateKey) print("[TEST]Sendig keys to keyserver..") message = 'REGISTER#' + str(2) + "#" + str(client1PublicKey) clientsocket_2.send(str.encode(message)) keyServerResponseProcesser(clientsocket_2) print("------------GETTING OTHER CLIENT PUBLIC KEY---------------") print( "Scenario:Client1 tries to get Client2 public key and port in order to communicate\n" ) message = 'GETPUBKEY#' + str(2) clientsocket_1.send(str.encode(message)) keyServerResponseProcesser(clientsocket_1) print( "------------GETTING NOT REGISTERED CLIENT PUBLIC KEY---------------") print("Scenario:Client1 tries to get a key which not exists\n") message = 'GETPUBKEY#' + str(555) clientsocket_1.send(str.encode(message)) keyServerResponseProcesser(clientsocket_1) clientsocket_1.close() clientsocket_2.close()
def run_merkle_hellman(): action = get_action() print("* Seed *") seed = input("Set Seed [enter for random]: ") import random if not seed: random.seed() else: random.seed(seed) print("* Building private key...") private_key = generate_private_key() public_key = create_public_key(private_key) if action == 'E': # Encrypt data = get_input(binary=True) print("* Transform *") chunks = encrypt_mh(data, public_key) output = ' '.join(map(str, chunks)) else: # Decrypt data = get_input(binary=False) chunks = [int(line.strip()) for line in data.split() if line.strip()] print("* Transform *") output = decrypt_mh(chunks, private_key) set_output(output)
def testKnapsackMessage(): print("1---KNAPSACK TEST FOR MESSAGE ENCRYPTION---") pk1 = Knapsack.generate_private_key() pubkey1 = Knapsack.create_public_key(pk1) pk2 = Knapsack.generate_private_key() pubkey2 = Knapsack.create_public_key(pk2) str1 = "Volt egyszer egy Elek ugy hivtak hogy Teszt Elek aki szerette a teszt eseteket" print("Message that will be encrypted=" + str1) encripteduzenet = Knapsack.encrypt_mh(str1, pubkey2) print("Encrypted message=" + str(encripteduzenet)) decriptaltuzenet = Knapsack.decrypt_mh(encripteduzenet, pk2) print("Decrypted message= " + str(decriptaltuzenet)) print("-----------------------------------------------\n")
def __init__(self, address=None): if address is None: # Creating a new wallet, we need to generate the priv and publ keys self.priv_key = generate_private_key() self.address = b32encode( generate_public_key(self.priv_key).to_string()).decode() else: self.address = address self.amount = 0
def testKnapsackDeck(): print("2---KNAPSACK TEST FOR DECK ENCRYPTION---") pk1 = Knapsack.generate_private_key() pubkey1 = Knapsack.create_public_key(pk1) pk2 = Knapsack.generate_private_key() pubkey2 = Knapsack.create_public_key(pk2) str1 = ' '.join( str(e) for e in [ 4, 19, 24, 18, 21, 25, 26, 5, 6, 20, 2, 23, 12, 17, 13, 27, 16, 8, 10, 14, 1, 22, 7, 11, 9, 15, 3 ]) print("Message that will be encrypted=" + str([ 4, 19, 24, 18, 21, 25, 26, 5, 6, 20, 2, 23, 12, 17, 13, 27, 16, 8, 10, 14, 1, 22, 7, 11, 9, 15, 3 ])) encripteduzenet = Knapsack.encrypt_mh(str1, pubkey2) print("Encrypted message=" + str(encripteduzenet)) decriptaltuzenet = Knapsack.decrypt_mh(encripteduzenet, pk2) print("Decrypted message= " + str(decriptaltuzenet)) print("-----------------------------------------------\n")
def registrate_public_key(Id): global PRIVATE_TCP_PORT PRIVATE_TCP_PORT = int(Id) global PRIVATE_KEY PRIVATE_KEY = crypto.generate_private_key() global PUBLIC_KEY PUBLIC_KEY = crypto.create_public_key(PRIVATE_KEY) print("PRIVATE_TCP_PORT ", PRIVATE_TCP_PORT) print("PRIVATE_KEY ", PRIVATE_KEY) print("PUBLIC_KEY ", PUBLIC_KEY) serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serverSocket.connect((TCP_IP, TCP_PORT)) recieve(serverSocket) mess = 'registerPubKey ' + str(Id) + ' ' + str(PUBLIC_KEY) mess = str.encode(mess) serverSocket.send(mess) recieve(serverSocket)
def run_merkle_hellman(encrypting, data): """Run the Merkle-Hellman knapsack cryptosystem.""" action = get_action() print("* Seed *") seed = input("Set Seed [enter for random]: ") if not seed: random.seed() else: random.seed(seed) print("* Building private key...") private_key = generate_private_key() public_key = create_public_key(private_key) if encrypting: # Encrypt print("* Transform *") chunks = encrypt_mh(data, public_key) return ' '.join(map(str, chunks)) else: # Decrypt chunks = [int(line.strip()) for line in data.split() if line.strip()] print("* Transform *") return decrypt_mh(chunks, private_key)
def setUp(self): self.PRIVATE_KEY = crypto.generate_private_key() self.PUBLIC_KEY = crypto.create_public_key(self.PRIVATE_KEY)
def GenerateKnapsackKeyPair(self): clientPrivateKey = Knapsack.generate_private_key(8) clientPublicKey = Knapsack.create_public_key(clientPrivateKey) return clientPrivateKey, clientPublicKey