Beispiel #1
0
 def sendFile(self,clientName,fileName):
     s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s.connect((HOST,PORT))
     requestObj = {}
     requestObj['choice'] = 'get-client-port'
     requestObj['name']=clientName
     s.sendall(pickle.dumps(requestObj))
     client_port = pickle.loads(s.recv(1024))['port']
     f = open(self.homeFolder +"/"+fileName,'rb')
     messageObj = {}
     messageObj['sender']=self.name
     messageObj['type'] = 'file'
     messageObj['filename']=fileName
     s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     self.dhke1=diffie_hellman.DiffieHellman(self.rollnum)
     s.connect((HOST,client_port))
     self.dhke1.initiate_key_exchange(s)
     plain_text=json.dumps(messageObj).encode('utf-8')
     cipher_text=crypto.encrypt_p2p(plain_text,self.dhke1.key1,self.dhke1.key2,self.dhke1.key3)
     s.sendall(cipher_text)
     print(fileName ," sent")
     l = f.read(1023)
     while (l):
         cipher_text=crypto.encrypt_p2p(l,self.dhke1.key1,self.dhke1.key2,self.dhke1.key3)
         s.sendall(cipher_text)
         l = f.read(1023)
     f.close()
     s.close()
def main():
    # shared elliptic curve system of examples
    ec = diffie_hellman.EC(1, 18, 19)
    g, _ = ec.at(7)
    assert ec.order(g) <= ec.q

    # ECDH usage
    dh = diffie_hellman.DiffieHellman(ec, g)

    apriv = 11
    apub = dh.gen(apriv)

    bpriv = 3
    bpub = dh.gen(bpriv)

    cpriv = 7
    cpub = dh.gen(cpriv)
    # same secret on each pair
    assert dh.secret(apriv, bpub) == dh.secret(bpriv, apub)
    assert dh.secret(apriv, cpub) == dh.secret(cpriv, apub)
    assert dh.secret(bpriv, cpub) == dh.secret(cpriv, bpub)

    # not same secret on other pair
    assert dh.secret(apriv, cpub) != dh.secret(apriv, bpub)
    assert dh.secret(bpriv, apub) != dh.secret(bpriv, cpub)
    assert dh.secret(cpriv, bpub) != dh.secret(cpriv, apub)

    print("Success!")
Beispiel #3
0
 def listen(self):
     print('Server Started Listening....')
     host = '127.0.0.1'
     server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
     server.bind((host,self.port))
     server.listen()
     while True:
         conn,addr=server.accept()
         #print(addr)
         recvieved_message=bytearray()
         while True:
             temp_recvieved_message=conn.recv(1024)
             recvieved_message+=temp_recvieved_message
             if len(temp_recvieved_message)<1024:
                 break
         #have to add decryption
         if recvieved_message==b'DHKE':
             self.dhke=diffie_hellman.DiffieHellman(self.rollnum)
             #print("OBJ created")
             self.dhke.party2_key_exchange(conn)
             encrypted_message=bytearray()
             while True:
                 temp_encrypted_message=conn.recv(1024)
                 encrypted_message+=temp_encrypted_message
                 if len(temp_encrypted_message)<1024:
                     break
             #decrypt the message here
             encrypted_message=bytes(encrypted_message)
             decrypted_data=crypto.decrypt_p2p(encrypted_message,self.dhke.key1,self.dhke.key2,self.dhke.key3)
             data = json.loads(decrypted_data)
             #print(data)
             if data['type']=='text':
                 print(data['sender'],':',data['msg'])
             else:
                 self.storeFile(conn,data,False,data['sender'])
         else:                       
             data = pickle.loads(recvieved_message)
             #print(data)
             if data['type']=='text':
                 decrypted_data = crypto.decrypt_group_message(data['encrypted'],self.grouplist[data['groupname']])
                 print("Group Message :"+data['groupname'])
                 print(decrypted_data.decode())
             else:
                 self.storeFile(conn,data,True,data['groupname'])
         conn.close()
Beispiel #4
0
 def message(self,clientName,msg):
     s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s.connect((HOST,PORT))
     requestObj = {}
     requestObj['choice'] = 'get-client-port'
     requestObj['name']=clientName
     s.sendall(pickle.dumps(requestObj))
     client_port = pickle.loads(s.recv(1024))['port']
     messageObj = {}
     messageObj['sender']=self.name
     messageObj['type'] = 'text'
     messageObj['msg']=msg
     s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     self.dhke1=diffie_hellman.DiffieHellman(self.rollnum)
     s.connect((HOST,client_port))
     self.dhke1.initiate_key_exchange(s)
     plain_text=json.dumps(messageObj).encode('utf-8')
     cipher_text=crypto.encrypt_p2p(plain_text,self.dhke1.key1,self.dhke1.key2,self.dhke1.key3)
     s.sendall(cipher_text)
     s.close()
Beispiel #5
0
def main():
    dh = diffie_hellman.DiffieHellman()

    a_pri, a_pub, a_p, a_g = dh.gen_key_pair()
    m_pri, m_pub, m_p, m_g = dh.gen_key_pair()
    b_pri, b_pub, b_p, b_g = dh.gen_key_pair()

    a_sk = dh.gen_sec_key(m_pub, a_pri)
    key_a = sha1.sha1(str(a_sk))[:16]
    iv_a = my_rand.my_rand_byte(16)

    b_sk = dh.gen_sec_key(m_pub, b_pri)
    key_b = sha1.sha1(str(b_sk))[:16]
    iv_b = my_rand.my_rand_byte(16)

    plain_ori = my_rand.my_rand_str(my_rand.my_rand(30, 10))

    print "Sending following text from A to B..."
    print "\t" + plain_ori
    cipher = cbc_mode.cbc_encrypt(aes_128.aes_128_encrypt, iv_a, plain_ori, key_a)

    print "Text intercepted by M is:"
    plain = cbc_mode.cbc_decrypt(aes_128.aes_128_decrypt, iv_a, cipher, key_a)
    print "\t" + plain

    print "Forwarding text from M to B..."
    cipher = cbc_mode.cbc_encrypt(aes_128.aes_128_encrypt, iv_b, plain, key_b)

    print "Text received by B is:"
    plain = cbc_mode.cbc_decrypt(aes_128.aes_128_decrypt, iv_b, cipher, key_b)
    print "\t" + plain

    if plain == plain_ori:
        print "OK"
    else:
        print "Fail"