示例#1
0
    def __init__(self):
        self.__p = part_1.gen_p()
        self.__q = part_1.gen_p()
        self.__c = part_1.gen_p()

        self.N = self.__p * self.__q
        self.d = part_3.modulo_inversion(self.__c, (self.__p - 1) * (self.__q - 1))
示例#2
0
    def __init__(self, message):
        self.message = message
        self.p, self.q = part_1.gen_p(), part_1.gen_p()

        self.n = self.p * self.q
        self.f = (self.p - 1) * (self.q - 1)
        self.d = part_1.gen_g(self.f)
        self.c = part_1.gcd(self.d, self.f)[1]
示例#3
0
    def __init__(self, message):
        self.p = part_1.gen_p()
        self.q = part_1.gen_p()
        self.message = SHA256.new(message).digest()

        self.n = self.p * self.q  # public
        self.f = (self.p - 1) * (self.q - 1)
        self.d = gen_coprime_integer_in_range(self.f, 1, self.f - 1)  # public
        self.c = modulo_inversion(self.d, self.f)  # secret

        self.signature = 0
示例#4
0
    def __init__(self, graph):
        self.graph = graph
        self.coded = np.zeros((8, 8), dtype=int)
        self.encrypted = np.zeros((8, 8), dtype=int)  # return

        self.p, self.q = part_1.gen_p(), part_1.gen_p()

        self.n = self.p * self.q
        self.f = (self.p - 1) * (self.q - 1)
        self.d = part_1.gen_g(self.f)
        self.c = part_1.gcd(self.d, self.f)[1]
示例#5
0
    def __init__(self, message):
        self.p = part_1.gen_p()
        self.g = gen_g(self.p)
        self.message = SHA256.new(message).digest()  # return

        self.x = random.randint(1, self.p - 1)
        self.open_key = fme(self.g, self.x, self.p)
        self.k = gen_coprime_integer_in_range(self.p - 1, 1, self.p - 1)
        self.r = fme(self.g, self.k, self.p)  # return

        self.u = 0
        self.s = 0  # return
示例#6
0
def elgamal_encryption(file_path):
    with open(file_path, 'rb') as file:
        message = file.read()
    file.close()

    p = part_1.gen_p()
    g = part_1.gen_g(p)

    c_b = random.randint(1, p - 1)
    d_b = fme(g, c_b, p)

    if int.from_bytes(message, 'big') >= p:
        k = [0] * len(message)
        r = [0] * len(message)
        e = [0] * len(message)
        tmp = [0] * len(message)

        for i in range(0, len(message)):
            k[i] = random.randint(1, p - 2)
            r[i] = fme(g, k[i], p)
            e[i] = message[i] * fme(d_b, k[i], p)
            tmp[i] = e[i] * fme(r[i], p - 1 - c_b, p)

        result = bytearray(tmp)

        with open('elgamal_encryption_output.gif', 'wb') as file:
            file.write(result)

        for i in range(0, len(message)):
            if tmp[i] == message[i]:
                print(tmp[i], ' == ', message[i])
                continue
            else:
                print(tmp[i], ' != ', message[i])
                print("Error!")
                return False
        print('Successful')
        return True
    else:
        k = random.randint(1, p - 2)
        r = fme(g, k, p)
        e = message * fme(d_b, k, p) % p
        mx = e * fme(r, p - 1 - c_b, p) % p
    if message == mx:
        print(message, ' == ', mx)
        print("Successful!")
        return True
    print(message, ' != ', mx)
    print("Error!")
    return False
示例#7
0
def shamir_protocol(file_path):
    with open(file_path, 'rb') as file:
        message = file.read()
    file.close()

    p = part_1.gen_p()

    c_a = gen_c(p)
    d_a = part_1.gcd(c_a, p - 1)[1]
    c_b = gen_c(p)
    d_b = part_1.gcd(c_b, p - 1)[1]

    if int.from_bytes(message, 'big') >= p:
        x1 = [0] * len(message)
        x2 = [0] * len(message)
        x3 = [0] * len(message)
        x4 = [0] * len(message)
        for i in range(0, len(message)):
            x1[i] = fme(message[i], c_a, p)
            x2[i] = fme(x1[i], c_b, p)
            x3[i] = fme(x2[i], d_a, p)
            x4[i] = fme(x3[i], d_b, p)
        output = bytearray(x4)
        with open("shamir_protocol_output.gif", 'wb') as file:
            file.write(output)
        for i in range(0, len(message)):
            if x4[i] == message[i]:
                print(x4[i], ' == ', message[i])
                continue
            else:
                print(x4[i], ' != ', message[i])
                print("Error!")
                return False
        print("Successful!")
        return True
    else:
        x1 = fme(int.from_bytes(message, 'big'), c_a, p)
        x2 = fme(x1, c_b, p)
        x3 = fme(x2, d_a, p)
        x4 = fme(x3, d_b, p)

        if not x4 == int.from_bytes(message, 'big'):
            print(x4, ' != ', message)
            print("Error!")
            return False
        print(x4, ' == ', message)
        print("Successful!")
        return True
示例#8
0
    def __init__(self, poker_room_key, name=''):
        self.balance = part_1.gen_p()
        self.id = random.randint(1, self.balance - 1)

        self.name = name
        self.cards = []

        self.k = random.randint(1, SALT_P - 1)
        self.open_key_1 = part_1.fast_modulo_exponentiation(
            SALT_G, self.k, SALT_P)  # link
        self.key = part_1.fast_modulo_exponentiation(poker_room_key, self.k,
                                                     SALT_P)  # link

        self.open_key_2 = part_1.fast_modulo_exponentiation(
            SALT_G, self.key, SALT_P)
        self.check()
示例#9
0
import part_1

SALT_P = part_1.gen_p()
SALT_G = part_1.gen_g(SALT_P)
示例#10
0
def gen_c(p):
    a = part_1.gen_p()
    while part_1.gcd(a, p - 1)[0] != 1:
        a = part_1.gen_p()
    return a