def to_bytes(m, l=n.bit_length()):
    return int.to_bytes(m, l, byteorder='big')
コード例 #2
0
def to_bytes(m, l=n.bit_length()):
    return int.to_bytes(m, l, byteorder='big')


def to_int(b):
    return int.from_bytes(b, byteorder='big')


def print_bounds(low, up):
    print("[" + str(low) + "," + str(up) + "]")


if __name__ == '__main__':

    n_len = n.bit_length()  #1024

    decimal.getcontext().prec = n_len + 1
    upper_bound = decimal.Decimal(n)
    lower_bound = decimal.Decimal(0)
    print_bounds(lower_bound, upper_bound)

    c = ciphertext
    for i in range(n_len):
        c = (pow(2, e, n) * c) % n

        oracle = remote(HOST, PORT)
        oracle.send(to_bytes(c, n_len))
        ans = oracle.recv(1024)

        if ans[0] == 1:
#test the connection with the server
# server = remote(HOST, PORT)
# server.send(to_bytes(ciphertext))
# bit = server.recv(1024)
# print(bit)
# server.close()

# init the bounds
upper_bound = n
lower_bound = 0
print_bounds(lower_bound, upper_bound)

# loop
m = ciphertext
for i in range(n.bit_length()):
    m = (pow(2, e, n) * m) % n

    # interact with the server
    server = remote(HOST, PORT)
    server.send(to_bytes(m))
    bit = server.recv(1024)
    server.close()
    print(bit)

    # update bounds based on the leaked LSB
    if bit[0] == 1:
        lower_bound = (upper_bound + lower_bound) // 2
    else:
        upper_bound = (upper_bound + lower_bound) // 2
    print_bounds(lower_bound, upper_bound)
def to_bytes(m,l=n.bit_length()):
    return int.to_bytes(m, l, byteorder='big')

def to_int(b):
    return int.from_bytes(b,byteorder='big')

def print_bounds(low, up):
    print("[" + str(low) + "," + str(up) + "]")




m = ciphertext

# define the upper bound with decimal
decimal.getcontext().prec = n.bit_length()
lower_bound = decimal.Decimal(0)
upper_bound = decimal.Decimal(n)
print_bounds(lower_bound,upper_bound)

# approximation loop
for i in range(n.bit_length()):

    m = (pow(2, e, n) * m) % n
    server = remote(HOST, PORT)
    server.send(to_bytes(m))
    bit = server.recv(1024)
    server.close()
    print(bit)

    if  bit[0] == 1: