Exemple #1
0
def quitchat(rec_uname):
    global username
    data_to_send = "{'cmd':'quitchat','rec_uname':'%s','from_uname':'%s'}"%(rec_uname,username)
    cipher = encryption.encrypt(data_to_send,"serverkey.pem",publickey=None)
    signature = encryption.signature(data_to_send,"keypriv")
    outp = "{'cipher':'%s','signature':'%s'}"%(cipher,signature)
    sock.send(outp)
Exemple #2
0
def login():
    uname = raw_input(bcolors.OKBLUE+"Enter username : "******"Enter Password : "******"{'cmd':'login','uname':'%s','passwd':'%s'}"%(uname,passwd)
        cipher = encryption.encrypt(data_to_send,"serverkey.pem",publickey=None)
        signature = encryption.signature(data_to_send,"keypriv")
        outp = "{'cipher':'%s','signature':'%s'}"%(cipher,signature)
        sock.sendall(outp)
    except Exception as e:
        print(bcolors.FAIL+"An error occured :("+bcolors.ENDC)
        print(e)
        return 0
    
    try:
        data = sock.recv(1024)
    except:
        print(bcolors.FAIL+"No response received from the server :("+bcolors.ENDC)
        return 0
        
    data = ast.literal_eval(data.encode("utf-8"))
    cipher = data["cipher"]
    signature = data["signature"]
    resp=""
    resp_type=""
    
    hex_decode = codecs.getdecoder("hex")
    cipher = hex_decode(cipher)[0]
    signature = hex_decode(signature)[0]
    
    
    f = open("serverkey.pem","r")
    publickey = f.read()
    f.close()
    #check authenticity now
    resp = encryption.decrypt(cipher,"keypriv.pem")
    authenticated = encryption.check_authenticity(resp,signature,publickey)
    
    if(authenticated==1):
        #authentication successful
        pass
    elif(authenticated==0):
        print(bcolors.FAIL+"Authenticity of the message can't be verified!"+bcolors.ENDC)
        return 0
    
    resp = ast.literal_eval(resp.encode())
    resp_type = resp["resp_type"]

    if resp_type=="SUCC":
        clear_screen()
        global username
        username = uname
        print(bcolors.OKGREEN+"Logged in as "+bcolors.BOLD+username+bcolors.ENDC)
        return 1
    
    elif resp_type=="FAIL":
        print(bcolors.FAIL+"Can't log in!"+bcolors.ENDC)
        return 0
Exemple #3
0
def sendmessage(msg, rec_uname):
    data_to_send = "{'cmd':'msg','from_uname':'%s','to_uname':'%s','msg':'%s'}" % (
        username, rec_uname, msg)
    cipher = encryption.encrypt(
        data_to_send, "serverkey.pem",
        publickey=None)  #encrypt with server's public key
    signature = encryption.signature(data_to_send, "keypriv")
    outp = "{'cipher':'%s','signature':'%s'}" % (cipher, signature)
    sock.send(outp)
Exemple #4
0
def show_online():
    try:
        data_to_send = "{'cmd':'showonline'}"
        cipher = encryption.encrypt(data_to_send,"serverkey.pem",publickey=None)
        signature = encryption.signature(data_to_send,"keypriv")
        outp = "{'cipher':'%s','signature':'%s'}"%(cipher,signature)
        sock.send(outp)

    except:
        print(bcolors.FAIL+"Couldn't communicate with the server :("+bcolors.ENDC)
        return 0    
Exemple #5
0
def send(resp, c):
    data_to_send = resp
    publickey = client_keys[c]
    cipher = encryption.encrypt(data_to_send, fname=None, publickey=publickey)
    signature = encryption.signature(data_to_send, "server_keypriv")
    outp = "{'cipher':'%s','signature':'%s'}" % (cipher, signature)
    sent = c.send(outp)

    if (sent == 0):
        print(bcolors.FAIL +
              "Can't send the data, connection closed by client" +
              bcolors.ENDC)
        return 0
Exemple #6
0
def register():
    while True:
        uname = raw_input(bcolors.OKBLUE + "Choose a username : "******"Enter Password : "******"Re-type Password : "******"Passwords donot match, try again." +
                  bcolors.ENDC)
    try:
        data_to_send = "{'cmd':'register','uname':'%s','passwd':'%s'}" % (
            uname, passwd)
        cipher = encryption.encrypt(
            data_to_send, "serverkey.pem",
            publickey=None)  #encrypt with server's public key
        signature = encryption.signature(data_to_send, "keypriv")
        outp = "{'cipher':'%s','signature':'%s'}" % (cipher, signature)

        sock.send(outp)
        global username
        username = uname
    except:
        print(bcolors.FAIL + "Couldn't communicate with the server :(" +
              bcolors.ENDC)
        return 0

    try:
        data = sock.recv(1024)
    except:
        print(bcolors.FAIL + "No response received from the server :(" +
              bcolors.ENDC)
        return 0

    data = ast.literal_eval(data.encode("utf-8"))
    cipher = data["cipher"]
    signature = data["signature"]
    resp = ""
    resp_type = ""

    hex_decode = codecs.getdecoder("hex")
    cipher = hex_decode(cipher)[0]
    signature = hex_decode(signature)[0]

    f = open("serverkey.pem", "rb")
    publickey = f.read()
    f.close()

    #check authenticity now
    resp = encryption.decrypt(cipher, "keypriv.pem")
    authenticated = encryption.check_authenticity(resp, signature, publickey)

    if (authenticated == 1):
        #authentication successful
        print("Autheticity verified")
    elif (authenticated == 0):
        print(bcolors.FAIL + "Authenticity of the message can't be verified!" +
              bcolors.ENDC)
        return 0

    resp = ast.literal_eval(resp.encode())
    resp_type = resp["resp_type"]

    if resp_type == "SUCC":
        global username
        username = uname
        print(bcolors.OKGREEN + "Logged in as " + bcolors.BOLD + username +
              bcolors.ENDC)
        return 1

    elif resp_type == "FAIL":
        print(bcolors.FAIL + "Can't register, try another username!" +
              bcolors.ENDC)
        return 0

    return 1
Exemple #7
0
def send():
    while True:
        inp = print_to_screen(2, bcolors.OKBLUE, ">>")
        if (not (inp == "" or inp == None)):
            if not (inp in commands):
                print_to_screen(1, bcolors.FAIL, "Invalid command!")
                continue

            elif inp == ":chat":
                global username
                if (username == None):
                    print_to_screen(
                        1, bcolors.FAIL,
                        "User is not logged in, or not properly configured!")
                    sys.exit(0)

                from_uname = username
                to_uname = print_to_screen(2, bcolors.OKBLUE,
                                           "Enter username to chat with >>")
                msg = "talk."

                try:
                    data_to_send = "{'cmd':'msg','from_uname':'%s','to_uname':'%s','msg':'%s'}" % (
                        from_uname, to_uname, msg)
                    cipher = encryption.encrypt(
                        data_to_send, "serverkey.pem",
                        publickey=None)  #encrypt with server's public key
                    signature = encryption.signature(
                        data_to_send,
                        "keypriv")  #sign with client's private key
                    outp = "{'cipher':'%s','signature':'%s'}" % (cipher,
                                                                 signature)
                    sock.send(outp)
                except Exception as e:
                    print_to_screen(1, bcolors.FAIL,
                                    "Couldn't communicate with the server!")
                    print(e)

                #open new chat window now
                startchat(2, to_uname, msg)

            elif inp == ":showonline":
                show_online()

            elif inp == ":logout":
                global username
                try:
                    data_to_send = "{'cmd':'logout','uname':'%s'}" % (username)
                    cipher = encryption.encrypt(
                        data_to_send, "serverkey.pem",
                        publickey=None)  #encrypt with server's public key
                    signature = encryption.signature(data_to_send, "keypriv")
                    outp = "{'cipher':'%s','signature':'%s'}" % (cipher,
                                                                 signature)
                    sent = sock.send(outp)

                    if (not (sent == 0)):
                        print(bcolors.OKGREEN + "Logged out succesfully!" +
                              bcolors.ENDC)
                    else:
                        print(bcolors.FAIL + "Can't logout" + bcolors.ENDC)

                except Exception as e:
                    print_to_screen(1, bcolors.FAIL,
                                    "Couldn't communicate with the server!")
                    print(e)

            else:
                try:
                    data_to_send = "{'cmd':'%s'}" % inp[1:len(inp)]
                    cipher = encryption.encrypt(
                        data_to_send, "serverkey.pem",
                        publickey=None)  #encrypt with server's public key
                    signature = encryption.signature(data_to_send, "keypriv")
                    outp = "{'cipher':'%s','signature':'%s'}" % (cipher,
                                                                 signature)
                    sent = sock.send(outp)
                except:
                    print_to_screen(1, bcolors.FAIL,
                                    "Couldn't communicate with the server!")

        else:
            continue