Esempio n. 1
0
def exchange_key(client):
    client.send(n.to_bytes(byte_size(n), bit_order))
    client.send(d.to_bytes(byte_size(d), bit_order))
    client_n = client.recv(1024)
    client_d = client.recv(1024)
    client_n = int.from_bytes(client_n, bit_order, signed=False)
    client_d = int.from_bytes(client_d, bit_order, signed=False)
    print(f"n {n}\n d {d}\n client_n {client_n} \n client_d {client_d} \n")
    return client_n, client_d
Esempio n. 2
0
def exchange_key():
    server_n = client.recv(2048)
    server_d = client.recv(2048)
    print("sending......")
    server_n = int.from_bytes(server_n, bit_order, signed=False)
    server_d = int.from_bytes(server_d, bit_order, signed=False)
    print(f"server n received {server_n}")
    print(f"server d received {server_d}")
    client.send(n.to_bytes(byte_size(n), bit_order))
    client.send(d.to_bytes(byte_size(d), bit_order))
    print(f"send n {n}")
    print(f"send d {d}")
    return server_n, server_d
Esempio n. 3
0
def recv_full_mess_RSA(socket, keys):
    """ Receive message encrypted with RSA in arbitrary size, decrypt it and return plaintext string

   :param socket: the socket to receive the message
   :param keys: public keys of the sender to decrypt message
   :return: plaintext string of decrypted message in arbitrary size
   """
    full_mess = b''
    key_len = utils.byte_size(keys['n'])
    new_mess = True
    mess = socket.recv(HEADER).decode(FORMAT)
    while True:
        if new_mess:
            len_mess = int(mess[:HEADER])
            new_mess = False
        mess = socket.recv(HEADER)
        full_mess += mess
        decrypted_mess = []
        if len(full_mess) == len_mess:
            if len_mess > key_len:
                len_chunk = key_len
                chunks = [
                    full_mess[i:(i + len_chunk)]
                    for i in range(0, len_mess, len_chunk)
                ]
                for chunk in chunks:
                    decrypted_mess.append(
                        decrypt(chunk, keys['n'], keys['d']).decode(FORMAT))
                return "".join(decrypted_mess)
            else:
                return decrypt(full_mess, keys['n'], keys['d']).decode(FORMAT)
Esempio n. 4
0
def send_mess_RSA(mess, socket, keys):
    """ Send message in arbitrary size with RSA encryption
    :param mess: the message to send. Must be a  string in arbitrary size
    :param socket: socket which fire the sending
    :param keys: private keys for RSA encryption
    :return: None
    """
    b_mess = bytes(mess, FORMAT)
    key_len = utils.byte_size(keys['n'])
    mess_len = len(b_mess)
    if mess_len >= key_len - 11:
        max_chunk = key_len - 11
        chunks = [
            b_mess[i:(i + max_chunk)] for i in range(0, mess_len, max_chunk)
        ]
        chunks.append(b_mess[-(mess_len % max_chunk):])
        full_mess = []
        for chunk in chunks:
            x = encrypt(chunk, keys['n'], e)
            full_mess.append(x)
        full_mess = b''.join(full_mess)
        len_mess = f'{len(full_mess):<{HEADER}}'
        full_mess = len_mess.encode(FORMAT) + full_mess
        socket.send(full_mess)
    else:
        mess = encrypt(b_mess, keys['n'], e)
        len_mess = f'{len(mess):<{HEADER}}'
        mess = bytes(len_mess, FORMAT) + mess
        socket.send(mess)
Esempio n. 5
0
def decrypt(cipher, n, d):
    keylen = byte_size(n)
    i_cipher = bytes_to_int(cipher)
    c_int = powmod(i_cipher, d, n)
    clear_mess = int_to_bytes(c_int, keylen)
    if len(cipher) > keylen:
        raise ValueError("Decryption failed")
    sep_idx = clear_mess.find(b'\x00', 2)
    return clear_mess[sep_idx + 1:]
Esempio n. 6
0
def encrypt(mess, n, e):
    """RSA encryption a message

    :param mess: bytes string mess
    :param n: modulo key
    :param e: private exponent key
    :return: Encrypted message with RSA in bytes string
    """
    keylen = byte_size(n)
    mess = pad_for_encryption(mess, keylen)
    m_int = bytes_to_int(mess)
    c_int = powmod(m_int, e, n)
    return int_to_bytes(c_int, keylen)
Esempio n. 7
0
def decrypt(cipher, n, d):
    """Decrypt message with RSA

    :param cipher: bytes string of cipher text
    :param n: modulo key
    :param d: public exponent key
    :return: Decrypted message with RSA in bytes string
    """
    keylen = byte_size(n)
    c_int = bytes_to_int(cipher)
    m_int = powmod(c_int, d, n)
    clear_mess = int_to_bytes(m_int, keylen)
    if len(cipher) > keylen:
        raise ValueError("Decryption failed")
    sep_idx = clear_mess.find(b'\x00', 2)
    return clear_mess[sep_idx + 1:]
Esempio n. 8
0
def encrypt(mess, n, e):
    keylen = byte_size(n)
    pad_for_encryption(mess, keylen)
    m_int = bytes_to_int(mess)
    c_int = powmod(m_int, e, n)
    return int_to_bytes(c_int, keylen)
Esempio n. 9
0
def decrypt(cipher, n, d):
    keylen = byte_size(n)
    i_cipher = bytes_to_int(cipher)
    c_int = powmod(i_cipher, d, n)
    clear_mess = int_to_bytes(c_int, keylen)
    if len(cipher) > keylen:
        raise ValueError("Decryption failed")
    sep_idx = clear_mess.find(b'\x00', 2)
    return clear_mess[sep_idx + 1:]


if __name__ == '__main__':
    # s, t = gen_prime(1024)
    # n, d = gen_rsa_para(1024)
    # mess = b'ggggggggg'
    # c = encrypt(mess, n, E)
    # m = decrypt(c, n, d)
    # print(m.decode(FORMAT))
    x = 100999
    t = x.to_bytes(byte_size(x), 'big')
    print(t)

    # mess = "hello dkhfkhsdof skfsdkh sdkhlf"
    # c = string_to_int(mess)
    # m = int_to_string(c, 31)
    # print(c)
    # print(m)
    # print(d)
    # print(f"{q} is prime after {c} times try")