Beispiel #1
0
 def run(self):
     Tw = sch.Trapdoor(key_pres.prikey, self.right_bar_widget_search_input.text())
     self.right_bar_widget_search_input.setText("")
     param=list(map(int, str(key_pres.pubkey).replace(')','').replace('(','').replace(',','').split(' ')))
     Tw = list(map(int, str(Tw).replace(')','').replace('(','').replace(',','').split(' ')))
     param = param + Tw
     #TODO:把Tw放在智能合约
     Contract1 = tosolc.getContract(tosolc.contract1_abi, "0xC6605fd9F15DbF27A115e74C0E94Ebc1ED2eE376")
     print('end')
     tosolc.createProject(Contract1, self.currentAccount, self.currentPassword, param) #发送请求并创建子合约
     search_amount = tosolc.getAmount(Contract1, self.currentAccount, self.currentPassword)
     thread = threading.Thread(target=self.process_search_impl, name=None,  args=(search_amount, self.currentAccount, self.currentPassword)) 
     thread.start()
     cipheri_str = self.s1.recv(4000).decode('utf-8')
     cipheri = list(map(int, cipheri_str.replace(')','').replace('(','').replace(',','').split(' ')))
     c1_1=(py_ecc.fields.bn128_FQ(int(cipheri[0])),py_ecc.fields.bn128_FQ(int(cipheri[1])))
     c1_2=(py_ecc.fields.bn128_FQ2((py_ecc.fields.bn128_FQ(int(cipheri[2])),py_ecc.fields.bn128_FQ(int(cipheri[3]))))\
         ,py_ecc.fields.bn128_FQ2((py_ecc.fields.bn128_FQ(int(cipheri[4])),py_ecc.fields.bn128_FQ(int(cipheri[5])))))
     c1_3=bn.FQ12(cipheri[6:18])
     c1_4=bn.FQ12(cipheri[18:])
     cipheri = CipherI(c1_1,c1_2,c1_3,c1_4)
     emptytable = {}
     m = sch.Dec(cipheri, key_pres.prikey, emptytable)
     head_struct = self.s2.recv(4)
     head_len = struct.unpack('i', head_struct)[0]
     data = self.s2.recv(head_len)
     head_dir = json.loads(data.decode('utf-8'))
     filesize_b = head_dir['filesize_bytes']
     filename = head_dir['filename']
     f = open(filename, 'ab+')
     print(filesize_b)
     recv_len = 0
     while recv_len < filesize_b:
         percent = recv_len / filesize_b
         if filesize_b - recv_len > buffsize:
             recv_mesg = self.s2.recv(buffsize)
             f.write(recv_mesg)
             recv_len += len(recv_mesg)
         else:
             recv_mesg = self.s2.recv(filesize_b - recv_len)
             recv_len += len(recv_mesg)
             f.write(recv_mesg)
     f.close()
     decrypt(filename,str(m).encode('utf-8'))
     self.signal1.emit()
     Contract2Address = tosolc.getProjects(Contract1, self.currentAccount, self.currentPassword)[search_amount-1]
     self.Contract2 = tosolc.getContract(tosolc.contract2_abi, Contract2Address)
Beispiel #2
0
def night_sec(user, key, mykey):
    global Nsec
    path = os.getcwd(
    ) + '/' + user + "/night.txt"  # if error happen it may be cuz the path was wrong
    try:
        with open(path, 'r+') as check:
            night = check.read()
            hmm = decrypt(night, mykey).decode()
            user_night = decrypt(key, hmm).decode()
            if user_night == hmm:
                Nsec = hmm
                return 1
            else:
                return 2

    except:
        return 3
Beispiel #3
0
def authentication(info, key):
    flag = 0
    global admin_stat
    user, password = info
    auth = open('admin.txt', 'r+')
    auth = decrypt(auth.read(), key)
    auth = auth.decode()
    # for admins in auth: #for admin
    admins = auth.split(':')
    for i in range(0, len(admins), 2):
        admin = admins[i]
        passwd = admins[1 + i]
        if admin == user and passwd == password:
            admin_stat = True
            flag = 1
            return user
        else:
            pass
    user_auth = open('user.txt', 'r+')
    for users in user_auth:  #for users
        users = users.split(':')
        while ('' in users):
            users.remove('')
        #users = user.remove('')
        name, passwd = users

        if name == user and passwd.replace('\n', '') == password:
            admin_stat = False
            flag = 1
            return user
        else:
            pass

    try:
        if flag == 0:
            msg = 'User or password did not found'
            return 0
        else:
            pass
            print('a pass worked')
    except:
        print('there was an error while checking the flag for the auth')
Beispiel #4
0
def main():
    print(ANSI_CYAN + "BETA [!]" + ANSI_RESET)
    global admin_stat
    global Nsec
    secure_flag = False
    #key = 'godisdead'
    port = 2468
    try:
        host = str(sys.argv[1])
        key = (sys.argv[2])
    except:
        print(
            'example : main.py 127.0.0.1 godisdead\n [python file name] [HOST][Night Key]'
        )
        sys.exit()
    print(ANSI_CYAN + "Using Key:{} Using :{} Port".format(key, port) +
          ANSI_RESET)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    s.listen(1)  # it will listen to only for one connection
    conn, addr = s.accept()
    #this is a beta it can handle only one connection and if the connection got closed the server will be down too
    with conn:
        print('Connection by {}'.format(addr))
        while True:
            ### note dont forget to send your public key so you can uncode the data
            #public_key = conn.recv(1024) # first ill recive a key from the client
            conn.send(
                b'Hello Guest, The connection is not encrypt yet...\nAlso you should have the Secret ;)\nEverything after this message will be encrypt...'
            )
            user = conn.recv(1024).decode()  # ill recive the user
            if len(user) > 0:
                conn.send(b'OK')
                night_secret = conn.recv(
                    1024).decode()  # ill recive the night secret here
                check = night_sec(user, night_secret, key)
                if check == 1:
                    secure_flag = True  # the user sent a valid night secret to do a secure connection with
                    msg = encrypt('OK', Nsec)
                    conn.send(msg)
                    secure_passd = conn.recv(1024).decode()
                    passd = decrypt(secure_passd, Nsec)
                    passd = passd.decode()
                    try:
                        info = user, passd
                        auth = authentication(info, key)
                        if admin_stat == True:
                            print("{} Loged in as Administrator ".format(auth))
                            msg = ('Welcome Administrator {}'.format(auth))
                            mesg = encrypt(msg, key)
                            conn.send(mesg)
                            while True:
                                stuff = conn.recv(1024)
                                ##stuff to do for admins
                                ord = decrypt(stuff, key)
                                ord = ord.decode()
                                if ord.find(":") != -1:
                                    ord = ord.split(':')
                                elif ord.find(":") == -1:
                                    pass
                                if type(ord) == list:
                                    try:
                                        if ord[0] == 'create account':
                                            if admin_stat == True:

                                                done = Administrator(
                                                    user, ord, key)
                                                msg = encrypt(done, key)
                                                conn.send(msg)
                                            else:
                                                msg = (
                                                    'You should have an Administrator privileges for that'
                                                )
                                                mesg = encrypt(msg, key)
                                                conn.send(mesg)
                                        elif ord[0] == 'send mail':
                                            try:
                                                done = normal_users(
                                                    user, ord
                                                )  #there is no need for the Administrator
                                                #privileges so normal_users will handle it"
                                                msg = encrypt(msg, key)
                                                conn.send(msg)
                                            except:
                                                msg = ('Unable to {}'.format(
                                                    ord[0]))
                                                mesg = encrypt(msg, key)
                                                conn.send(mesg)
                                        elif ord[0] == 'delete account':

                                            if admin_stat == True:
                                                done = Administrator(
                                                    user, ord, key)
                                                msg = encrypt(done, key)
                                                conn.send(mesg)

                                            else:

                                                try:
                                                    info = ord[1], ord[2]
                                                    checking_user = authentication(
                                                        info, key)
                                                    if checking_user == ord[1]:

                                                        done = normal_users(
                                                            user, ord)
                                                        conn.close(
                                                        )  #there is no need for telling the user we done just close the connection
                                                    else:
                                                        pass
                                                except:
                                                    msg = ('Unable to {}'.
                                                           format(ord))
                                                    mesg = encrypt(msg, key)
                                                    conn.send(mesg)
                                    except:
                                        print('error while handling the order')
                                elif type(ord) != list:
                                    if ord == "inbox":
                                        done = normal_users(user, ord)
                                        msg = encrypt(done, key)
                                        conn.send(msg)
                                else:
                                    msg = (
                                        'server could not understand your order {}'
                                        .format(ord))
                                    mesg = encrypt(msg, key)
                                    conn.send(mesg)
                        elif (admin_stat == False) and (auth != 0):
                            msg = ('Welcome {}'.format(auth))
                            mesg = encrypt(msg, key)
                            conn.send(mesg)
                            while True:
                                stuff = conn.recv(1024)
                                #stuff to do for users
                                ord = decrypt(stuff, key)
                                ord = ord.decode()
                                if ord.find(":") != -1:
                                    ord = ord.split(':')
                                elif ord.find(":") == -1:
                                    pass
                                if type(ord) == list:
                                    try:
                                        if ord[0] == 'create account':
                                            msg = (
                                                'You should have an Administrator privileges for that'
                                            )
                                            mesg = encrypt(msg, key)
                                            conn.send(mesg)
                                        elif ord[0] == 'send mail':
                                            try:
                                                done = normal_users(
                                                    user, ord
                                                )  #there is no need for the Administrator
                                                #privileges so normal_users will handle it"
                                                msg = encrypt(msg, key)
                                                conn.send(msg)
                                            except:
                                                msg = ('Unable to {}'.format(
                                                    ord[0]))
                                                mesg = encrypt(msg, key)
                                                conn.send(mesg)
                                        elif ord[0] == 'delete account':
                                            try:
                                                info = ord[1], ord[2]
                                                checking_user = authentication(
                                                    info, key)
                                                if checking_user == ord[1]:
                                                    done = normal_users(
                                                        user, ord)
                                                    conn.close(
                                                    )  #there is no need for telling the user we done just close the connection
                                                else:
                                                    pass
                                            except:
                                                msg = (
                                                    'Unable to {}'.format(ord))
                                                mesg = encrypt(msg, key)
                                                conn.send(mesg)
                                    except:
                                        print('error while handling the order')
                                elif type(ord) != list:
                                    if ord == "inbox":
                                        done = normal_users(user, ord)
                                        msg = encrypt(done, key)
                                        conn.send(msg)
                        else:
                            break  ## TODO: add a blocking ips
                    except:
                        print(
                            'there was an error while checking someone login {}'
                            .format(user))
                elif check == 3:
                    print('Could not open [{}] night secret'.format(user))
                    conn.send(b"we don't like you")
                    s.close()
                else:  #CONNECTION IS NOT SECURE AT ALL ADMIN SHOULD TAKE ACTION HERE
                    secure_flag = False
                    conn.send('NOT found').encode()
                    print(
                        'Uh no f**k you im out, there was no secure connection so i dropped it'
                    )
                    s.close()
                    #sys.exit()
            else:
                user = ''
                night_secret = ''
                print('user was not vaild')
                s.close()
Beispiel #5
0
def test_decrypt():
    assert convert_stream_to_plaintext(decrypt("69c4e0d86a7b0430d8cdb78070b4c55a", "000102030405060708090a0b0c0d0e0f")) == "00112233445566778899aabbccddeeff"
Beispiel #6
0
def main():
    port = 2468
    host = str(sys.argv[1])
    print("Using {}:{} ".fromat(host,port))
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((host,port))
    try:
        wlc_msg = s.recv(1024).decode()
        print(wlc_msg)
        user = input('User :'******'OK':
            secret= input('Your secret :')
            night_secret = encrypt(secret,secret)
            s.send(night_secret)
            respon = s.recv(1024).decode()
            try:
                respon = decrypt(respon,secret).decode() 
                if respon == 'OK':
                    password = input('Password:'******':>')
                        try:
                            if len(cmd) < 1 :
                                cmd = input('Type something :>')
                        except:
                            pass
                        if cmd == 'help':
                            help()
                        elif cmd == 'create account':
                            username = input('account user:'******'Password for the user:'******'Night secret for the user:'******'create account'+':'+username+':'+user_night+':'+user_password)
                            command = encrypt(cmd,secret)
                            s.send(command)
                            s.settimeout(3.5)
                            server_resposn = s.recv(1024).decode()
                            server_resposn = decrypt(server_resposn,secret)
                            print(server_resposn.decode())
                        elif cmd == 'delete account':
                            username = input('account user:'******'Password:'******'delete account'+':'+username+':'+user_password)
                            command = encrypt(cmd,secret)
                            s.send(command)
                            s.settimeout(3.5)
                            server_resposn = s.recv(1024).decode()
                            server_resposn = decrypt(server_resposn,secret)
                            print(server_resposn)
                        elif cmd == 'inbox':
                            cmd = encrypt(cmd,secret)
                            s.send(cmd)
                            s.settimeout(3.5)
                            server_resposn = s.recv(1024).decode()
                            server_resposn = decrypt(server_resposn,secret)
                            print(server_resposn)
                        elif cmd == 'send mail':
                            username = input('User to send to :')
                            my_mail = input('Your mail:')
                            cmd = ('send mail'+':'+username+':'+my_mail)
                            command = encrypt(cmd,secret)
                            s.send(command)
                            s.settimeout(3.5)
                            server_resposn = s.recv(1024).decode()
                            server_resposn = decrypt(server_resposn,secret)
                            print(server_resposn)
                        
                    
                        
            except:
                print('Server cloesd the connection')
        else:
            print('server respond with an error')
        
    except:
        print('Could not be able to make connection with the server !')
    print("\nKey Details has been sent...")
    incoming_message = conn.recv(1024)
    incoming_message = incoming_message.decode()
    print("\nPUBLIC KEY FOR B = " + incoming_message)
    actualKey = exc(keyArray[0], keyArray[2], int(incoming_message))
    print("\nKEY = " + actualKey)
    print("\nEncrypted Message = ")
    encrypted_msg = encrypt(message, str(actualKey))
    print(encrypted_msg)
    conn.send(encrypted_msg.encode())

    incoming_message = conn.recv(1024)
    incoming_message = incoming_message.decode()
    print(incoming_message)
    trl1 = incoming_message.split('[')
    trl1 = trl1[1].split(']')
    trl1 = trl1[0].split(',')
    print(int(trl1[0]))
    keyArray = keyGeneration_B(int(trl1[0]), int(trl1[1]))
    print(keyArray)
    actualKey = exc(int(trl1[0]), keyArray[0], int(trl1[2]))
    message = str(int(keyArray[1]))
    message = message.encode()
    conn.send(message)
    print("\nKEY = " + actualKey)
    incoming_message = conn.recv(1024)
    incoming_message = incoming_message.decode()
    print("\nReceived Message = " + str(incoming_message))
    print("\nActual Msg = ")
    print(decrypt(str(incoming_message), str(actualKey)))
def main():
    curve = ellipticCurve(
        q=
        0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409,
        a=
        0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC,
        b=
        0x0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00,
        p=2**521 - 1)
    '''
    curve = ellipticCurve(a=0x340E7BE2A280EB74E2BE61BADA745D97E8F7C300,b=0x1E589A8595423412134FAA2DBDEC95C8D8675E58,p=0xE95E4A5F737059DC60DFC7AD95B3D8139515620F
, q=0xE95E4A5F737059DC60DF5991D45029409E60FC09)
    '''
    temp = 'D4-6D-6D-A0-96-B4'.replace('-', '')
    alice = binascii.unhexlify(temp)
    print(alice)
    temp = '00-FF-F4-EF-53-FE'.replace('-', '')
    bob = binascii.unhexlify(temp)
    print(bob)
    password = '******'.encode()
    print(password)
    PE = hunting_and_pecking_with_ecc(curve, alice, bob, password)
    alice_scalar, alice_element, alice_private = createPrivateAndMask(PE)
    bob_scalar, bob_element, bob_private = createPrivateAndMask(PE)
    #ellipticCurvePoint(10, 13, curve)
    #ここから鍵交換プロトコルを使う
    temp1 = PE.mul(bob_scalar)
    temp2 = temp1.add(bob_element)
    temp3 = temp2.mul(alice_private)
    #print(temp1, temp2, temp3)
    #print("temp3.x =",temp3.x)
    alice_ss = (temp3.x).to_bytes(math.ceil(math.log2(temp3.x) / 8), 'big')
    #alice_ss = hex((PE.mul(bob_scalar).add(bob_element).mul(alice_private)).x)
    print("alice_ss =", alice_ss)
    temp1 = PE.mul(alice_scalar)
    temp2 = temp1.add(alice_element)
    temp3 = temp2.mul(bob_private)
    #print(temp1, temp2, temp3)
    #print("temp3.x =",temp3.x)
    bob_ss = (temp3.x).to_bytes(math.ceil(math.log2(temp3.x) / 8), 'big')
    print("bob_ss = ", bob_ss)

    # commit
    ## alice
    n = len(list(map(int, format(PE.curve.p, "b")))) * 2
    temp = hashlib.shake_256(alice_ss +
                             b'Dragonfly Key Derivation').hexdigest(512)
    alice_kck = temp[0:256]
    alice_mk = temp[256:512]
    print("alice_kck =", alice_kck)
    print("alice_mk =", alice_mk)
    ## bob
    temp = hashlib.shake_256(bob_ss +
                             b"Dragonfly Key Derivation").hexdigest(512)
    bob_kck = temp[0:256]
    bob_mk = temp[256:512]
    print("bob_kck =", bob_kck)
    print("bob_mk =", bob_mk)

    assert (alice_kck == bob_kck)

    #交換する前に自分のconfirmの値を求める
    ##alice側
    alice_confirm = confirm(alice_scalar, bob_scalar, alice_element,
                            bob_element, alice_kck, alice)
    ##bob側
    bob_confirm = confirm(bob_scalar, alice_scalar, bob_element, alice_element,
                          bob_kck, bob)
    #交換して,相手のconfirmの値が正しいか検証する
    ##alice側
    bob_confirm_expected = confirm(bob_scalar, alice_scalar, bob_element,
                                   alice_element, bob_kck, bob)
    ##bob側
    alice_confirm_expected = confirm(alice_scalar, bob_scalar, alice_element,
                                     bob_element, alice_kck, alice)

    #それぞれで計算結果と送られてきた結果を比較
    ##alice側
    if bob_confirm == bob_confirm_expected:
        print("alice: confirm successed")

    ##bob側
    if alice_confirm == alice_confirm_expected:
        print("bob: confirm successed")

    data = "@elliptic_shiho"
    print("data =", data)
    #bob側
    ciphertext, iv = encrypt(bob_mk[0:32].encode(), data)
    #alice側
    plaintext = decrypt(alice_mk[0:32].encode(), ciphertext, iv)

    print("enc_data =", plaintext.decode())

    data = "㍾㍽㍼㍻㋿(⋈◍>◡<◍)。✧♡💗😻💑💕🌠🌟🔥"
    print("data =", data)
    #alice側
    ciphertext, iv = encrypt(alice_mk[0:32].encode(), data)
    #bob側
    plaintext = decrypt(bob_mk[0:32].encode(), ciphertext, iv)
    print("enc_data =", plaintext.decode())
Beispiel #9
0
def revealFunc(CM_HM,password):
    SM_extract=extractFunc(CM_HM)
    print("Your secret message:",decrypt(password,SM_extract))
    return decrypt(password,SM_extract)