コード例 #1
0
 def generate_key(self, fd):
     self._send_message(fd, b"GENERATE_KEY")
     b = crypto.gen_big_prime(KEY_LENGTH)
     data4key = self._read_message(fd)
     data4key = unpack_message(data4key)
     g, p, A = from_binary(data4key['g']), from_binary(
         data4key['p']), from_binary(data4key['A'])
     B = powmod(g, b, p)
     self._send_message(fd, pack_message(to_binary(B)))
     KEY = powmod(A, b, p)
     KEY = to_binary(KEY)
     self.KEY = KEY
コード例 #2
0
    def gmpy_deserialize(obj: bytes, **_kwargs: Any) -> GmpyTypes:
        r"""
        Function for deserializing gmpy objects

        :param obj: object to deserialize
        :param \**_kwargs: optional extra keyword arguments
        :return: deserialized gmpy object
        """
        return gmpy2.from_binary(obj)
コード例 #3
0
    def handle(self):
        # self.request is the TCP socket connected to the client
        # generate key
        global a

        command = read_message(self.request)
        if command == b"GENERATE_KEY":
            data4key = {
                'g': to_binary(G),
                'p': to_binary(P),
                'A': to_binary(powmod(G, a, P))
            }
            send_message(self.request, pack_message(data4key))
            data4key = read_message(self.request)
            KEY = powmod(from_binary(unpack_message(data4key)), a, P)
            print("key created: ", KEY)
            KEY = to_binary(KEY)

            sess_key = to_binary(crypto.get_random_bytes(SESS_KEY_LENGTH))
            send_message(self.request, sess_key)

            data = unpack_message(read_message(self.request))
            account = db.auth(data[0], data[1], sess_key)
            send_message(self.request, pack_message(account))
            if account is not None:
                db.set_sesskey(account[1], binascii.b2a_hex(KEY).decode())

            a = crypto.gen_big_prime(KEY_LENGTH)
            print("<bin key>", KEY)
        elif command == b"COMMUNICATION":
            login = unpack_message(read_message(self.request))[0]
            KEY = db.get_sesskey(login)
            if KEY is None:
                print("No Sess Key")
                self.request.close()
                return
            KEY = binascii.a2b_hex(KEY)
            print("<key from db>", KEY)
            rc4 = ARC4.new(KEY)
            data = read_message(self.request)
            decrypted = rc4.decrypt(data)
            d = unpack_message(decrypted)

            if 'cmd' in d and 'args' in d:
                #print(d['args'])
                res = funcs[d['cmd']](*d['args'])
                data2recv = pack_message(res)
                encrypted_res = rc4.encrypt(data2recv)
                send_message(self.request, encrypted_res)
            else:
                data2recv = pack_message(b"null")
                encrypted_res = rc4.encrypt(data2recv)
                send_message(self.request, encrypted_res)
            self.request.close()
コード例 #4
0
def bytes_to_q(b: bytes) -> ElementModQ:
    """
    Returns an element from a byte sequence.
    """
    return ElementModQ(mpz(from_binary(b)))