def test_ElGamal(num_bit, message): print("\n------------------------------------------------------") # Generate random prime numbers p = BlumBlumShub.generate_prime(num_bit) # Test generate_keypair which returns public key ( g, p, h) and the private key (g, p, x) public, private = ElGamal.generate_keypair(p) print("\n------------------------------------------------------") encrypted_msg = ElGamal.encrypt(public, message) print("\n------------------------------------------------------") decrypted_msg = ElGamal.decrypt(private, encrypted_msg) print("\n------------------------------------------------------") eve_decrypted_msg = ElGamal.eve(public, encrypted_msg) return decrypted_msg, eve_decrypted_msg
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket.connect(SERVER) print('Waiting for input public key ...') recvline = '' sendline = '' pk_li = [] while True: recvline = socket.recv(4096).decode() if recvline != '': pk_li = recvline.split(' ') pk = tuple([int(x) for x in pk_li]) assert (len(pk) == 3) print('public key: ', pk) socket.send('OK!'.encode('UTF-8')) break line = '' while line != 'bye': # 標準入力からデータを取得 print('Enter your message here.') line = input('> ') # サーバに送信 socket.send(pickle.dumps(ElGamal.encrypt(line, pk))) # 暗号化 -> シリアライズ socket.close() print('Client side closed.')
def ElGamalEncryption(p, q, g, y, message): cipher = ElGamal.encrypt(p, q, g, y, message) r, t = cipher[0], cipher[1] return r, t
try: _recvline = _sock.recv(4096) print(_recvline.decode()) except Exception as e: print(e) break if __name__ == '__main__': socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST = '127.0.0.1' PORT = 55580 socket.connect((HOST, PORT)) print('Waiting for input public key ...') pk_li = [] while True: recvline = socket.recv(4096).decode() if recvline != '': pk_li = recvline.split(' ') pk = tuple([int(x) for x in pk_li]) assert (len(pk) == 3) print('public key: ', pk) break while True: your_input = input('') socket.send(pickle.dumps(ElGamal.encrypt(your_input, pk))) # 暗号化 -> シリアライズ thread = threading.Thread(target=Handler, args=(socket,), daemon=True) thread.start()
if type_encryption == "1": print(" public :", public) result = [int(x.strip()) for x in public.split(',')] print(" result :", result) public_key = (result[0]), (result[1]) # Encrypt with RSA Algorithm RSA.encrypt(public_key, message) # If encryption algorithm is El-Gamal elif type_encryption == "2": result = [int(x.strip()) for x in public.split(',')] # Encrypt with El-Gamal Algorithm ElGamal.encrypt((result[0], result[1], result[2]), message) # Otherwise, function 3 - Decrypt a message elif function == "3": # Get the encrypted message from user encrypted_msg = input("Please enter encrypted message : ") # Get the private key from user private = input( "Please enter the private key ((g, p, x) for El-Gamal / (d,n) for RSA ): " ) print( "\n------------------------------------------------------" )