コード例 #1
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
コード例 #2
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
コード例 #3
0
def listen():
    while True:
        try:
            data = sock.recv(1024)
            if not (data):
                print_to_screen(1, bcolors.FAIL,
                                "Connection terminated by the server :(")
                return 1

            data = ast.literal_eval(data.encode("utf-8"))
            cipher = data["cipher"]
            signature = data["signature"]
            hex_decode = codecs.getdecoder("hex")
            cipher = hex_decode(cipher)[0]
            signature = hex_decode(signature)[0]
            resp = ""
            resp_type = ""

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

            #check authenticity now
            resp = encryption.decrypt(cipher,
                                      "keypriv.pem")  #decrypt with private key
            authenticated = encryption.check_authenticity(
                resp, signature, publickey)  #public key of server

            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("utf-8"))

            resp_type = resp["resp_type"]

            if (resp_type == "FAIL"):
                err_msg = resp["resp"]
                print_to_screen(
                    1, bcolors.FAIL,
                    "An Error Occured - " + bcolors.OKBLUE + "%s" % (err_msg))

            elif (resp_type == "SUCC"):
                succ_msg = resp["resp"]
                clear_screen()
                print_to_screen(
                    1, bcolors.OKGREEN,
                    "Success - " + bcolors.OKBLUE + "%s" % (succ_msg))
                print("\n")

            elif (resp_type == "msg"):
                global chats
                from_uname = resp["from_uname"]
                msg = resp["msg"]

                time.sleep(1)

                if (from_uname in chats):
                    startchat(1, from_uname, msg)
                else:
                    print_to_screen(
                        1, bcolors.OKBLUE,
                        "A new message received from %s, hit enter to see" %
                        from_uname)
                    open_msg = print_to_screen(2, bcolors.OKBLUE,
                                               "Open?(Y/N) : ")
                    while True:
                        if (open_msg.upper() == "Y"):
                            startchat(1, from_uname, msg)
                            break
                        elif (open_msg.upper() == "N"):
                            break
                        else:
                            print_to_screen(
                                1, bcolors.FAIL,
                                "Invalid option, please enter again.")

            elif (resp_type == "users"):
                online_users = resp["resp"]
                print_to_screen(1, bcolors.OKBLUE,
                                "\nONLINE USERS : \n" + bcolors.ENDC)
                users = ""
                for i in online_users:
                    if (i == ""):
                        continue
                    users = users + str(i) + "\n"
                print_to_screen(1, bcolors.FAIL, users)

            elif (resp_type == "quitchat"):
                uname = resp["resp"]
                if (uname in chats):
                    c_destination = chats[uname]
                    c_destination.send(":quitchat")

            else:
                pass

        except KeyboardInterrupt:
            print(bcolors.FAIL + "Connection closed by user." + bcolors.ENDC)
            return 1
            sys.exit(0)
            break
コード例 #4
0
def new_connection(c, a):
    #Accept data from the client
    while True:
        try:
            data = c.recv(1024)
        except Exception as e:
            print("Error - %s" % e)

        if (not (data)):
            print("Connection closed by client.\n")
            del connections[connections.index(c)]
            for i in authorized_users:
                if authorized_users[i] == c:
                    del authorized_users[i]
                    break
            break

        else:
            try:

                if "BEGIN PUBLIC KEY" in data:
                    #handshaking stage
                    client_keys[c] = data.encode(
                    )  #add client public key to dictionary
                    print("Handshaking..")
                    f = open("server_keypriv.pem", "rb")
                    server_publickey = f.read()
                    f.close()
                    c.send(server_publickey)
                    print("Sent public key")

                else:
                    #print("\nData received : %s\n"%data)
                    data = ast.literal_eval(data)
                    #first check authenticity
                    cipher = data["cipher"]
                    signature = data["signature"]
                    publickey = client_keys[c].encode()
                    decode_hex = codecs.getdecoder("hex")
                    signature = decode_hex(signature)[0]
                    cipher = decode_hex(cipher)[0]
                    #check authenticity now
                    req = encryption.decrypt(cipher, "server_keypriv.pem")
                    publickey = client_keys[c]
                    authenticated = encryption.check_authenticity(
                        req, signature, publickey)  #public key of client
                    req = ast.literal_eval(req.encode("utf-8"))
                    if (authenticated == 1):
                        #authentication successful
                        cmd = req['cmd']

                        if (cmd == 'login'):
                            login(req['uname'], req['passwd'], c)

                        elif (cmd == 'register'):
                            register(req['uname'], req['passwd'], c)

                        elif (cmd == 'msg'):
                            sendmessage(req['from_uname'], req['to_uname'],
                                        req['msg'])

                        elif (cmd == 'showonline'):
                            showonlineusers(c)

                        elif (cmd == 'logout'):
                            logout(req["uname"])

                        elif (cmd == 'quitchat'):
                            rec_uname = req["rec_uname"]
                            from_uname = req["from_uname"]
                            #we need to tell rec_uname that from_uname has left the chat
                            for i in authorized_users:
                                if i == rec_uname:
                                    quitchat(i, from_uname)

                        else:
                            outp = "{'resp_type':'FAIL','resp':'Invalid command'}"
                            send(outp, c)

                    elif (authenticated == 0):
                        print(
                            bcolors.FAIL +
                            "Authenticity of the message can't be verified!" +
                            bcolors.ENDC)

            except Exception as e:
                print("Wrong format.")
                print(e)