def register(message, signature):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    try:
        type = b'reg'
        pysocket.send_msg(sock, type)
        pysocket.send_msg(sock, message)
        pysocket.send_msg(sock, signature)
        succes = pysocket.recv_msg(sock)
        if succes == b'#succesRG':
            certificate = pysocket.recv_msg(sock)
            if certificate != b'#invalid':
                signature = pysocket.recv_msg(sock)
                sock.close()
                return certificate, signature
            else:
                sock.close()
                return b'invalid', b'invalid'
        else:
            sock.close()
            return b'#error', b'#error'

    finally:
        sock.close()
def request_payment(user, message, signature):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    try:
        type = b'request_payment'
        pysocket.send_msg(sock, type)
        pysocket.send_msg(sock, user.encode())
        pysocket.send_msg(sock, message)
        pysocket.send_msg(sock, signature)
        succes = pysocket.recv_msg(sock)
        if succes == b'#succesTM':
            print("succes TM")
        else:
            sock.close()
            print(succes)

    finally:
        sock.close()
def upload_hash(user, message, signature):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    try:
        type = b'upload_hash'
        pysocket.send_msg(sock, type)
        pysocket.send_msg(sock, user)
        pysocket.send_msg(sock, message)
        pysocket.send_msg(sock, signature)
        succes = pysocket.recv_msg(sock)
        if succes == b'#succesUH':
            print("succes UH")
        else:
            sock.close()
            print(succes)

    finally:
        sock.close()
def comm(message, signature):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10002)
    sock.connect(server_address)
    try:
        type = b'com'
        pysocket.send_msg(sock, type)
        pysocket.send_msg(sock, message)
        pysocket.send_msg(sock, signature)
        succes = pysocket.recv_msg(sock)
        if succes == b'valid comm':
            return True, sock
        else:
            sock.close()
            return False
    finally:
        print("succes new comm")
def register_seller(message, signature):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    try:
        type = b'reg_seller'
        pysocket.send_msg(sock, type)
        pysocket.send_msg(sock, message)
        pysocket.send_msg(sock, signature)
        succes = pysocket.recv_msg(sock)
        if succes == b'#succesRGS':
            print("succes RGS")
        else:
            sock.close()
            print(succes)

    finally:
        sock.close()
def request_currency(message, signature):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    try:
        type = b'req_currency'
        pysocket.send_msg(sock, type)
        pysocket.send_msg(sock, message)
        pysocket.send_msg(sock, signature)
        succes = pysocket.recv_msg(sock)
        if succes == b'#succesRC':
            print("succes RC")
            return pysocket.recv_msg(sock)
        else:
            sock.close()
            print(succes)

    finally:
        sock.close()
def payment(sock, c1, c3, c5):
    i1 = c1[1]
    i3 = c3[1]
    i5 = c5[1]
    try:

        while True:
            if len(c1[0]) - i1 - 1 == 0 and len(c3[0]) - i3 - 1 == 0 and len(
                    c5[0]) - i5 - 1 == 0:
                print("No more funds in the account. Closing operation")
                return False, i1, i3, i5
            value = int(input("Value centi = "))
            while value - 1 > (len(c1[0]) - i1 - 1) + (
                    len(c3[0]) - i3 - 1) * 3 + (len(c5[0]) - i5 - 1) * 5:
                print("Value too big")
                value = int(input("Value centi = "))
            if value <= 0:
                print("Closing operation")
                return True, i1, i3, i5
            type = b'pay'
            pysocket.send_msg(sock, type)
            payment_representation = get_payment_representation(
                value,
                len(c1[0]) - i1 - 1,
                len(c3[0]) - i3 - 1,
                len(c5[0]) - i5 - 1)
            print([
                len(c1[0]) - i1 - 1,
                len(c3[0]) - i3 - 1,
                len(c5[0]) - i5 - 1
            ])
            print(payment_representation)
            if payment_representation[0] != 0 or payment_representation[
                    1] != 0 or payment_representation[2] != 0:
                payment_representation_bytes = [
                    [
                        c1[0][payment_representation[0] + i1],
                        payment_representation[0]
                    ],
                    [
                        c3[0][payment_representation[1] + i3],
                        payment_representation[1]
                    ],
                    [
                        c5[0][payment_representation[2] + i5],
                        payment_representation[2]
                    ]
                ]
            else:
                print("No more funds in the account. Closing operation")
                return False, i1, i3, i5
            payment_representation_bytes = pickle.dumps(
                payment_representation_bytes, -1)

            pysocket.send_msg(sock, payment_representation_bytes)
            succes = pysocket.recv_msg(sock)
            if succes == b'valid payment':
                i1 += payment_representation[0]
                i3 += payment_representation[1]
                i5 += payment_representation[2]
                print([
                    len(c1[0]) - i1 - 1,
                    len(c3[0]) - i3 - 1,
                    len(c5[0]) - i5 - 1
                ])
                print("Valid payment")
            else:
                print("Invalid payment")
    except:
        print("error")
Example #8
0
 if type == b'com':
     #angajamentul primit de la user
     message = pysocket.recv_msg(connection)
     semnatura = pysocket.recv_msg(connection)
     data1 = message
     data = pickle.loads(data1)
     c01 = data["c0"][0]
     c03 = data["c0"][1]
     c05 = data["c0"][2]
     cCU = data["cCU"]
     sCU = data["sCU"]
     cCOMM = message
     sCOMM = semnatura
     if not sign.auth_sign(message, keyserver_methods.req(
             data["U"]), semnatura):
         pysocket.send_msg(connection, b'invalid user sign')
         connection.close()
         break
     if not sign.auth_sign(data["cCU"],
                           keyserver_methods.req("banca"),
                           data["sCU"]):
         pysocket.send_msg(connection, b'invalid bank sign')
         connection.close()
         break
     currentdate = str(now.strftime("%Y-%m-%d"))
     currentdate = parser.parse(currentdate)
     if currentdate > parser.parse(data["d"]):
         pysocket.send_msg(connection, b'date expired')
         connection.close()
         break
     pysocket.send_msg(connection, b'valid comm')
Example #9
0
            data1=message
            data=pickle.loads(data1)
            #completam structura cu datele necesare pentru user
            mydata["U"]=data["U"]
            database[data["U"]]={"currency":data["currency"],"pub_key":data["key"]}
            mydata["Uip"]=client_address
            difference2 = datetime.timedelta(weeks=5)
            mydata["exp"]=now+difference2
            mydata["info"]={'serial':hash_functions.random_with_N_digits(10),'cred_limit':30000}
            data_string = pickle.dumps(mydata, -1)


            signature=pysocket.recv_msg(connection) #receptionam semnatura
            signature=crypt.decrypt(signature,private_key.exportKey("PEM"))
            if sign.auth_sign(message, keyserver_methods.req(data["U"]), signature): #verificam semnatura userului conform cheii publice primite de la server
                pysocket.send_msg(connection,b'#succesRG') #confirmam userului ca semnatura e valida
                pysocket.send_msg(connection,crypt.encrypt(data_string,keyserver_methods.req(data["U"])))# trimitem certificatul
                signature=sign.get_sign(data_string,private_key) # semnam certificatul
                pysocket.send_msg(connection,crypt.encrypt(signature,keyserver_methods.req(data["U"])))#trimitem certificatul semnat
                connection.close()
            else:
                pysocket.send_msg(sock,b'#notsucces') #in caz ca semnatura nu e valida
                connection.close()
        if type==b'reg_seller':
            #datele primite de la seller
            message=pysocket.recv_msg(connection)
            data1=message
            data=pickle.loads(data1)
            database[data["V"]]={"currency":data["currency"],"pub_key":data["key"]}

            signature=pysocket.recv_msg(connection) #receptionam semnatura
Example #10
0
    crypt.encrypt(signature_for_hash, keyserver_methods.req("banca")))

angajament = {"V": "vanzator"}
angajament["U"] = nume
angajament["cCU"] = certificat
angajament["sCU"] = semnatura
angajament["c0"] = (lant1[0][0], lant2[0][0], lant3[0][0])
angajament["d"] = str(now.strftime("%Y-%m-%d"))
angajament["info"] = hash_functions.int_to_bytes(n)

data_string_for_seller = pickle.dumps(angajament, -1)

signature_for_seller = sign.get_sign(data_string_for_seller, private_key)

succes, sock = seller_methods.comm(data_string_for_seller,
                                   signature_for_seller)
while succes:
    j, lant1[1], lant2[1], lant3[1] = seller_methods.payment(
        sock, lant1, lant2, lant3)
    if not j:
        pysocket.send_msg(sock, b'close')
        sock.close()
        break
    x = input("Close session?: ")
    if x == 'yes':
        pysocket.send_msg(sock, b'close')
        sock.close()
        break
else:
    print("Comm invalid")